英文:
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 == "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.
答案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("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: 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("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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论