在Golang中文件上传失败

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

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,

&lt;div class=&quot;form-input upload-file&quot; enctype=&quot;multipart/form-data&quot; &gt;
	&lt;input type=&quot;file&quot;name=&quot;file&quot; id=&quot;file&quot; /&gt;
    &lt;input type=&quot;hidden&quot;name=&quot;token&quot; value=&quot;{{.}}&quot; /&gt;
   	&lt;a href=&quot;/uploadfile/&quot; data-toggle=&quot;tooltip&quot; title=&quot;upload&quot;&gt;
    	&lt;input type=&quot;button upload-video&quot; class=&quot;btn btn-primary btn-filled btn-xs&quot; value=&quot;upload&quot; /&gt;
    &lt;/a&gt;
&lt;/div&gt;

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(&quot;file&quot;)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}
	defer file.Close()

	out, err := os.Create(&quot;/tmp/uploadedfile&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)
}

When I try to upload the file, I am getting request Content-Type isn&#39;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.

&lt;form action=&quot;/uploadfile/&quot; enctype=&quot;multipart/form-data&quot; method=&quot;post&quot;&gt;
    &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot; /&gt;
    &lt;input type=&quot;hidden&quot;name=&quot;token&quot; value=&quot;{{.}}&quot; /&gt;
    &lt;input type=&quot;submit&quot; value=&quot;upload&quot; /&gt;
&lt;/form&gt;

答案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(&quot;Content-Type&quot;, writer.FormDataContentType())

This is included in the mime/multipart package.

For a working example please check this blog post.

huangapple
  • 本文由 发表于 2016年3月6日 20:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/35826612.html
匿名

发表评论

匿名网友

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

确定