Ajax Upload file to GoLang server with Multipart content type
I am trying to upload an audio file to a Golang server using a multi-page form. However, Go returns an error:
multipart: NextPart: bufio: buffer full
I believe this indicates that there is something not in multipart 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 am getting this error, I would really appreciate it.
source to share
After a long, hard fight with the Ajax request, I got it to send the correct information. Here is 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);
Please note that the header is no longer used and I found that you can attach data to formdata much easier than any other method, for example: How do I submit form content for multiple forms / forms over ajax (no jquery)?
source to share
Not sure if this answer works for you, but I was able to upload the file via ajax using form-data
client side and the following small piece 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)
There r
is *http.Request
. Postscript it just saves the file in the same folder and doesn't perform any security checks.
source to share