使用Multipart内容类型将文件通过Ajax上传到GoLang服务器

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

Ajax Upload File to GoLang Server with Content Type Multipart

问题

我正在尝试使用多部分表单将音频文件上传到一个使用Golang服务器的服务器。然而,Go返回以下错误:

multipart: NextPart: bufio: buffer full

我认为这表明我的Javascript请求中有一些不符合多部分格式的内容。这是我的Javascript代码:

function UploadFile(file) {
	var xhr = new XMLHttpRequest();


	if (file.type == "audio/mpeg" && file.size <= $id("MAX_FILE_SIZE").value) {
		// start upload
		var boundary = '---------------------------' + Math.floor(Math.random()*32768) + Math.floor(Math.random()*32768) + Math.floor(Math.random()*32768);

		xhr.open("POST", $id("upload").action, true);
		xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
		xhr.setRequestHeader("X_FILENAME", file.name);
		xhr.send(file);
	}
}

这是我的Golang服务器处理程序:

func FileHandler(w http.ResponseWriter, r *http.Request) {
	var (
		status int
		err    error
	)
	defer func() {
		if nil != err {
			http.Error(w, err.Error(), status)
		}
	}()
	// parse request with maximum memory of _24Kilobits
	const _24K = (1 << 20) * 24
	if err = r.ParseMultipartForm(_24K); nil != err {
		fmt.Println(err)
		status = http.StatusInternalServerError
		return
	}
	for _, fheaders := range r.MultipartForm.File {
		for _, hdr := range fheaders {
			// open uploaded
			var infile multipart.File
			if infile, err = hdr.Open(); nil != err {
				status = http.StatusInternalServerError
				return
			}
			// open destination
			var outfile *os.File
			if outfile, err = os.Create("./uploaded/" + hdr.Filename); nil != err {
				status = http.StatusInternalServerError
				return
			}
			// 32K buffer copy
			var written int64
			if written, err = io.Copy(outfile, infile); nil != err {
				status = http.StatusInternalServerError
				return
			}
		w.Write([]byte("uploaded file:" + hdr.Filename + ";length:" + strconv.Itoa(int(written))))
		}
	}
}

如果有人知道我为什么会收到这个错误,我将非常感激。

英文:

I'm trying to upload an audio file to a Golang server using a multipart form. However, Go returns the error:

multipart: NextPart: bufio: buffer full

I believe this indicates there is something not in the multipart format with my Javascript request. This is my Javascript:

function UploadFile(file) {
	var xhr = new XMLHttpRequest();


	if (file.type == &quot;audio/mpeg&quot; &amp;&amp; file.size &lt;= $id(&quot;MAX_FILE_SIZE&quot;).value) {
		// start upload
		var boundary = &#39;---------------------------&#39; + Math.floor(Math.random()*32768) + Math.floor(Math.random()*32768) + Math.floor(Math.random()*32768);

		xhr.open(&quot;POST&quot;, $id(&quot;upload&quot;).action, true);
		xhr.setRequestHeader(&quot;Content-Type&quot;, &quot;multipart/form-data; boundary=&quot; + boundary);
		xhr.setRequestHeader(&quot;X_FILENAME&quot;, file.name);
		xhr.send(file);
	}
}

And this is my Golang server handler:

func FileHandler(w http.ResponseWriter, r *http.Request) {
	var (
		status int
		err    error
	)
	defer func() {
		if nil != err {
			http.Error(w, err.Error(), status)
		}
	}()
	// parse request with maximum memory of _24Kilobits
	const _24K = (1 &lt;&lt; 20) * 24
	if err = r.ParseMultipartForm(_24K); nil != err {
		fmt.Println(err)
		status = http.StatusInternalServerError
		return
	}
	for _, fheaders := range r.MultipartForm.File {
		for _, hdr := range fheaders {
			// open uploaded
			var infile multipart.File
			if infile, err = hdr.Open(); nil != err {
				status = http.StatusInternalServerError
				return
			}
			// open destination
			var outfile *os.File
			if outfile, err = os.Create(&quot;./uploaded/&quot; + hdr.Filename); nil != err {
				status = http.StatusInternalServerError
				return
			}
			// 32K buffer copy
			var written int64
			if written, err = io.Copy(outfile, infile); nil != err {
				status = http.StatusInternalServerError
				return
			}
		w.Write([]byte(&quot;uploaded file:&quot; + hdr.Filename + &quot;;length:&quot; + strconv.Itoa(int(written))))
		}
	}
}

If anyone has any ideas why I'm getting this error, I'd greatly appreciate it.

答案1

得分: 3

经过长时间的与Ajax请求的艰苦战斗,我成功发送了正确的信息。以下是我使用的代码:

var xhr = new XMLHttpRequest(),
    boundary=Math.random().toString().substr(2);

var formdata = new FormData();
formdata.append("file", file);

xhr.open("POST", $id("upload").action, true);
//xhr.setRequestHeader("content-type", "multipart/form-data; charset=utf-8; boundary=" + boundary);
xhr.send(formdata);

请注意,头部信息不再使用,并且我发现你可以更轻松地将数据附加到formdata,而不是使用其他任何方法,例如这个链接中的方法:https://stackoverflow.com/questions/5933949/how-to-send-multipart-form-data-form-content-by-ajax-no-jquery#5935286

英文:

After a long, arduous battle with the Ajax request, I got it to send the right information. Here's the code I used:

var xhr = new XMLHttpRequest(),
    boundary=Math.random().toString().substr(2);

var formdata = new FormData();
formdata.append(&quot;file&quot;, file);

xhr.open(&quot;POST&quot;, $id(&quot;upload&quot;).action, true);
//xhr.setRequestHeader(&quot;content-type&quot;, &quot;multipart/form-data; charset=utf-8; boundary=&quot; + boundary);
xhr.send(formdata);

Note the header is no longer in use and I found you can attach data to formdata much easier than any other method such as this: https://stackoverflow.com/questions/5933949/how-to-send-multipart-form-data-form-content-by-ajax-no-jquery#5935286

答案2

得分: 0

不确定这个答案是否适合你,但是我能够使用客户端的form-data通过ajax上传文件,并在服务器端使用以下简短的Go代码片段:

file, handler, err := r.FormFile("img") // img是form-data的键
if err != nil {
    fmt.Println(err)
    return
}
defer file.Close()

fmt.Println("文件正常")
fmt.Println(handler.Filename)
fmt.Println()
fmt.Println(handler.Header)

f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
    fmt.Println(err)
    return
}
defer f.Close()
io.Copy(f, file)

这里的r*http.Request。附注:这只是将文件存储在相同的文件夹中,并不执行任何安全检查。

英文:

Not sure whether this answer suits you, but I was able to upload the file via ajax using form-data on a client side and the following small snippet of Go code on the server:

file, handler, err := r.FormFile(&quot;img&quot;) // img is the key of the form-data
if err != nil {
	fmt.Println(err)
	return
}
defer file.Close()

fmt.Println(&quot;File is good&quot;)
fmt.Println(handler.Filename)
fmt.Println()
fmt.Println(handler.Header)


f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
	fmt.Println(err)
	return
}
defer f.Close()
io.Copy(f, file)

Here r is *http.Request. P.S. this just stores the file in the same folder and does not perform any security checks.

huangapple
  • 本文由 发表于 2014年8月26日 04:17:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25493706.html
匿名

发表评论

匿名网友

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

确定