本文介绍了通过xmlhtt prequest上传文件生成的多部分:下一页部分:EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过上传XMLHtt prequest文件,目前我使用golang

当我使用curl测试上传一切顺利,该文件被上传到亚马逊的S3存储,但使用Javascript,当我收到以下错误:多部分:下一页部分:EOF

这是JS的一部分:

 的document.getElementById(附件)。的addEventListener(变,功能(五){
    var文件= this.files [0];
    VAR XHR =新XMLHtt prequest();
    VAR FORMDATA =新FORMDATA();
    formdata.append(附件,文件);
    。边界=的Math.random()的toString()SUBSTR(2);

    xhr.onreadystatechange =功能(E){
        如果(4 == this.readyState){
            的console.log([XHR上传完成,E]);
        }
    };
    xhr.open(POST,https://开头upload_host:8443 /上载,真正的);
    xhr.setRequestHeader(内容类型,多部分/格式数据;字符集= UTF-8;边界= ------------------------- - +边界+;);
    xhr.send(FORMDATA);
}, 假);
 

有关在旅途中处理程序:

  FUNC upload_handler(W http.ResponseWriter,R * http.Request,男render.Render){
  w.Header()设置(访问控制 - 允许 - 起源,*)
  文件头,错误:= r.FormFile(C.Storage.FieldName)
  如果犯错!=零{
    //错误时,抛出HERE
    ServeHTTP(400,err.Error(),米)
    返回
  }

  内容错误:= ioutil.ReadAll(文件)

  如果犯错!=零{
    ServeHTTP(400,err.Error(),米)
    返回
  }

  作品尺寸,ERR:= file.Seek(0,2)// 2 =从端
  如果犯错!=零{
    ServeHTTP(400,err.Error(),米)
    返回
  }

  如果文件大小> Int64的(C.Storage.FileSize){
    ServeHTTP(400,文件大小超过限制,M)
    返回
  }

  FTYPE:= http.DetectContentType(内容)

  如果!strings.Contains(C.Storage.AllowedMimes,FTYPE){
    ServeHTTP(400,文件类型不允许的,M)
    返回
  }

  //功能上传到Amazon S3

  file.Close()
}
 

解决方案

有关上传你可以使用PlUploader $ C $℃实施例的(即还用于大文件上传)。

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()
}
解决方案

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

这篇关于通过xmlhtt prequest上传文件生成的多部分:下一页部分:EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:34