Pass filename of the file selected for upload using enctype="multipart/form-data" to a struct field in Golang

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

Pass filename of the file selected for upload using enctype="multipart/form-data" to a struct field in Golang

问题

我的应用程序使用HTML代码片段来上传文件的表单。

<form method="POST" action="/addproduct" enctype="multipart/form-data">
    <label class="form-control-label" for="productimage"></label>
    {{with .Errors.image}}
    <div class="alert alert-danger">
        {{.}}
    </div>
    {{end}}		

    <input type="file" name="productimage" id="productimage" multiple="multiple" class="btn btn-danger">

    <input type="submit" name="submit" value="Submit" class="btn btn-info">
</form>

我需要获取上传文件的文件名,并将其传递给Golang中的结构字段。

file, header, err := r.FormFile("productimage")
defer file.Close()
if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
}
var pimage = header.Filename

p := &Product{
    Puid:     Puid(),
    Pname:    r.FormValue("productName"),
    Quantity: r.FormValue("quantity"),
    Price:    r.FormValue("price"),
    Image:    pimage,
}

我正在尝试将所选文件的名称传递给结构体Productimage字段。有关如何完成此操作的建议吗?

英文:

My application uses the html code snippet for the form to upload a file

&lt;form  method=&quot;POST&quot; action=&quot;/addproduct&quot; enctype=&quot;multipart/form-data&quot;&gt;
        &lt;label class=&quot;form-control-label&quot; for=&quot;productimage&quot;&gt;&lt;/label&gt;
        {{with .Errors.image}}
        &lt;div class=&quot;alert alert-danger&quot;&gt;
            {{.}}
        &lt;/div&gt;
        {{end}}		
		
		&lt;input type=&quot;file&quot; name=&quot;productimage&quot; id = &quot;productimage&quot; multiple=&quot;multiple&quot; class = &quot;btn btn-danger&quot;&gt;
					
        &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; class = &quot;btn btn-info&quot;&gt;
    &lt;/form&gt;

I need to grab the filename of the uploaded file and pass it to a struct field in Golang.

	file, header, err := r.FormFile(&quot;productimage&quot;)
	defer file.Close()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	var pimage = header.Filename 

p := &amp;Product{
	Puid:     Puid(),
	Pname:    r.FormValue(&quot;productName&quot;),
	Quantity: r.FormValue(&quot;quantity&quot;),
	Price:    r.FormValue(&quot;price&quot;),
	Image:    pimage,
}

I am trying to pass the name of the file selected for uploaded to 'image' field of the struct 'Product'. Any suggestions on how this can be done?

答案1

得分: 0

你可以尝试使用以下代码替代r.FormFile()的调用:

mpr, _ := r.MultipartReader()
filePart, _ := r.NextPart()
fileName := filePart.FileName()

不过,我建议你检查错误情况 Pass filename of the file selected for upload using enctype="multipart/form-data" to a struct field in Golang

英文:

Instead of calling r.FormFile(), you could instead try:

mpr, _ := r.MultipartReader()
filePart, _ := r.NextPart()
fileName := filePart.FileName()

However, I would check the errors Pass filename of the file selected for upload using enctype="multipart/form-data" to a struct field in Golang

huangapple
  • 本文由 发表于 2017年1月28日 17:15:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/41908120.html
匿名

发表评论

匿名网友

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

确定