使用beego上传具有相同格式的文件。

huangapple go评论86阅读模式
英文:

upload file with same format using beego

问题

文件上传已完成,但文件名不同于原始文件名。我在HTML文件中尝试了以下代码:

<html>
<title>Go upload</title>
<body>

<form action="http://localhost:8080/receive" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

在Beego/Go的receive.go文件中,我使用了以下代码:

package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
)

func uploadHandler(w http.ResponseWriter, r *http.Request) {
	file, header, err := r.FormFile("file")

	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	defer file.Close()

	out, err := os.Create("/home/vijay/Desktop/uploadfile")
	if err != nil {
		fmt.Fprintf(w, "无法创建文件,请检查写入权限")
		return
	}

	defer out.Close()

	_, err = io.Copy(out, file)
	if err != nil {
		fmt.Fprintln(w, err)
	}

	fmt.Fprintf(w, "文件上传成功:")
	fmt.Fprintf(w, header.Filename)
}

func main() {
	http.HandleFunc("/", uploadHandler)
	http.ListenAndServe(":8080", nil)
}

我能够实现文件上传,但文件名不同。现在我想要以与文件名相同的名称上传文件。

英文:

File Uploading is done but not with the same name as filename
i have tried this in html file

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

&lt;html&gt;
 &lt;title&gt;Go upload&lt;/title&gt;
 &lt;body&gt;

 &lt;form action=&quot;http://localhost:8080/receive&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
 &lt;label for=&quot;file&quot;&gt;Filename:&lt;/label&gt;
 &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot;&gt;
 &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;
 &lt;/form&gt;

 &lt;/body&gt;
 &lt;/html&gt;

<!-- end snippet -->

and in beego/go receive.go

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

package main

import (
	&quot;fmt&quot;
	&quot;io&quot;
	&quot;net/http&quot;
	&quot;os&quot;
)

func uploadHandler(w http.ResponseWriter, r *http.Request) {

	// the FormFile function takes in the POST input id file
	file, header, err := r.FormFile(&quot;file&quot;)

	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	defer file.Close()

	out, err := os.Create(&quot;/home/vijay/Desktop/uploadfile&quot;)
	if err != nil {
		fmt.Fprintf(w, &quot;Unable to create the file for writing. Check your write access privilege&quot;)
		return
	}

	defer out.Close()

	// write the content from POST to the file
	_, err = io.Copy(out, file)
	if err != nil {
		fmt.Fprintln(w, err)
	}

	fmt.Fprintf(w, &quot;File uploaded successfully : &quot;)
	fmt.Fprintf(w, header.Filename)
}

func main() {
	http.HandleFunc(&quot;/&quot;, uploadHandler)
	http.ListenAndServe(&quot;:8080&quot;, nil)
}

<!-- end snippet -->

i am able to achieve uploading file here with same format... but not with the same name..
now i want this file to be uploaded with same name as file name

答案1

得分: 7

你没有使用Beego控制器来处理上传。

package controllers

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (this *MainController) GetFiles() {
    this.TplNames = "aTemplateFile.html"

    file, header, er := this.GetFile("file") // 这里的this是控制器,file是你表单字段的id
    if file != nil {
        // 获取文件名
        fileName := header.Filename
        // 保存到服务器
        err := this.SaveToFile("file", somePathOnServer)
    }
}

请注意,我只翻译了代码部分,其他内容不包括在内。

英文:

You are not using a Beego controller to handle the upload

package controllers

import (
        &quot;github.com/astaxie/beego&quot;
)

type MainController struct {
        beego.Controller
}

function (this *MainController) GetFiles() {
    this.TplNames = &quot;aTemplateFile.html&quot;

    file, header, er := this.GetFile(&quot;file&quot;) // where &lt;&lt;this&gt;&gt; is the controller and &lt;&lt;file&gt;&gt; the id of your form field
    if file != nil {
        // get the filename
        fileName := header.Filename
        // save to server
        err := this.SaveToFile(&quot;file&quot;, somePathOnServer)
    }
}

huangapple
  • 本文由 发表于 2015年8月31日 20:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/32310838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定