通过XMLHttpRequest上传文件会生成多部分:NextPart:EOF

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

uploading file via xmlhttprequest generates multipart: NextPart: EOF

问题

我正在尝试通过XMLHttpRequest上传文件,目前我正在使用Golang。

当我使用CURL测试上传时,一切都正常,文件被上传到Amazon S3存储桶,但是当使用JavaScript时,我收到以下错误:multipart: NextPart: EOF

这是JavaScript部分的代码:

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 {
    // 在这里抛出错误
    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
  }
  
  // 上传到Amazon S3的函数

  file.Close()
}

以上是要翻译的内容。

英文:

I'm trying to upload a file via XMLHttpRequest, currently i'm using golang

When I use CURL to test the upload everything goes fine, and the file is uploaded to Amazon S3 bucket, but when using Javascript I get the following error : multipart: NextPart: EOF

this is the JS part :

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);

For the go handler :

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

得分: 1

你可以使用PlUploader进行上传(也可用于大文件上传)。代码示例

英文:

For uploading you can use PlUploader Code example (that is also used for large file uploading).

huangapple
  • 本文由 发表于 2015年8月25日 19:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/32202699.html
匿名

发表评论

匿名网友

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

确定