admin管理员组文章数量:1414908
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 == "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);
}
}
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 << 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))))
}
}
}
If anyone has any ideas why I'm getting this error, I'd greatly appreciate it.
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 == "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);
}
}
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 << 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))))
}
}
}
If anyone has any ideas why I'm getting this error, I'd greatly appreciate it.
Share Improve this question asked Aug 25, 2014 at 20:17 RjdleeRjdlee 7791 gold badge6 silver badges23 bronze badges2 Answers
Reset to default 3After 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("file", file);
xhr.open("POST", $id("upload").action, true);
//xhr.setRequestHeader("content-type", "multipart/form-data; charset=utf-8; boundary=" + 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: How to send multipart/form-data form content by ajax (no jquery)?
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("img") // img is the key of the form-data
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fmt.Println("File is good")
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.
本文标签: javascriptAjax Upload File to GoLang Server with Content Type MultipartStack Overflow
版权声明:本文标题:javascript - Ajax Upload File to GoLang Server with Content Type Multipart - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745166341a2645703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论