我正在尝试通过 XMLHttpRequest 上传文件,目前我正在使用 golang当我使用 CURL 测试上传时,一切正常,文件上传到 Amazon S3 存储桶,但在使用 Javascript 时出现以下错误: multipart: NextPart: EOF这是 JS 部分:document.getElementById("attachment").addEventListener("change", function(e) { var file = this.files[0]; var xhr = new XMLHttpRequest(); var formdata = new FormData(); formdata.append("attachment", file); boundary=Math.random().toString().substr(2); xhr.onreadystatechange = function(e) { if ( 4 == this.readyState ) { console.log(["xhr upload complete", e]); } }; xhr.open("POST", "https://upload_host:8443/upload", true); xhr.setRequestHeader("Content-Type","multipart/form-data; charset=utf-8; boundary=---------------------------"+boundary+";"); xhr.send(formdata);}, false);对于 go 处理程序:func upload_handler(w http.ResponseWriter, r *http.Request, m render.Render) { w.Header().Set("Access-Control-Allow-Origin", "*") file, header, err := r.FormFile("C.Storage.FieldName") if err != nil { // ERROR THROWN HERE ServeHTTP(400, err.Error(), m) return } content, err := ioutil.ReadAll(file) if err != nil { ServeHTTP(400, err.Error(), m) return } fileSize, err := file.Seek(0, 2) //2 = from end if err != nil { ServeHTTP(400, err.Error(), m) return } if fileSize > int64(C.Storage.FileSize) { ServeHTTP(400, "File size limit exceeded", m) return } ftype := http.DetectContentType(content) if !strings.Contains(C.Storage.AllowedMimes, ftype) { ServeHTTP(400, "File type not allowed", m) return } //FUNCTION TO UPLOAD TO AMAZON S3 file.Close()}
- 1 回答
- 0 关注
- 216 浏览
添加回答
举报
0/150
提交
取消