英文:
File upload failing in golang
问题
在我的用例中,我正在尝试在golang中将文件上传到服务器。我有以下HTML代码:
<div class="form-input upload-file" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="hidden" name="token" value="{{.}}" />
<a href="/uploadfile/" data-toggle="tooltip" title="upload">
<input type="button" class="btn btn-primary btn-filled btn-xs" value="upload" />
</a>
</div>
服务器端代码如下:
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// FormFile函数接收POST输入的文件
file, header, err := r.FormFile("file")
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
out, err := os.Create("/tmp/uploadedfile")
if err != nil {
fmt.Fprintf(w, "无法创建文件进行写入,请检查写入权限")
return
}
defer out.Close()
// 将POST内容写入文件
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
}
fmt.Fprintf(w, "文件上传成功:")
fmt.Fprintf(w, header.Filename)
}
当我尝试上传文件时,服务器端报错request Content-Type isn't multipart/form-data
。
有人可以帮助我解决这个问题吗?
英文:
In my use case I am trying to upload a file to server in golang. I have the following html code,
<div class="form-input upload-file" enctype="multipart/form-data" >
<input type="file"name="file" id="file" />
<input type="hidden"name="token" value="{{.}}" />
<a href="/uploadfile/" data-toggle="tooltip" title="upload">
<input type="button upload-video" class="btn btn-primary btn-filled btn-xs" value="upload" />
</a>
</div>
And the server side,
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// the FormFile function takes in the POST input id file
file, header, err := r.FormFile("file")
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
out, err := os.Create("/tmp/uploadedfile")
if err != nil {
fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
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, "File uploaded successfully : ")
fmt.Fprintf(w, header.Filename)
}
When I try to upload the file, I am getting request Content-Type isn't multipart/form-data
error in server side.
Could anyone help me on this?
答案1
得分: 3
说实话,我不知道你是如何得到错误的,因为你的HTML不是一个表单。但我认为你得到错误是因为默认情况下,表单会以GET请求发送,而multipart/form-data
应该通过POST发送。以下是一个最简单的表单示例,应该可以工作。
<form action="/uploadfile/" enctype="multipart/form-data" method="post">
<input type="file" name="file" id="file" />
<input type="hidden" name="token" value="{{.}}" />
<input type="submit" value="upload" />
</form>
英文:
To be honest I have no idea how do you even get error as your HTML is not form. But I think you getting error because by default form is send as GET request while multipart/form-data
should be send via POST. Here is example of minimal form which should work.
<form action="/uploadfile/" enctype="multipart/form-data" method="post">
<input type="file" name="file" id="file" />
<input type="hidden"name="token" value="{{.}}" />
<input type="submit" value="upload" />
</form>
答案2
得分: 0
问题是你必须包含包含内容类型的头部。
req.Header.Add("Content-Type", writer.FormDataContentType())
这个头部包含在 mime/multipart
包中。
请参考这篇博客文章,了解一个可工作的示例。
英文:
The issue is that you must include the header that contains the content type.
req.Header.Add("Content-Type", writer.FormDataContentType())
This is included in the mime/multipart
package.
For a working example please check this blog post.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论