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

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

uploading file via xmlhttprequest generates multipart: NextPart: EOF

问题

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

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

这是JavaScript部分的代码:

  1. document.getElementById("attachment").addEventListener("change", function(e) {
  2. var file = this.files[0];
  3. var xhr = new XMLHttpRequest();
  4. var formdata = new FormData();
  5. formdata.append("attachment", file);
  6. boundary=Math.random().toString().substr(2);
  7. xhr.onreadystatechange = function(e) {
  8. if ( 4 == this.readyState ) {
  9. console.log(["xhr upload complete", e]);
  10. }
  11. };
  12. xhr.open("POST", "https://upload_host:8443/upload", true);
  13. xhr.setRequestHeader("Content-Type","multipart/form-data; charset=utf-8; boundary=---------------------------"+boundary+";");
  14. xhr.send(formdata);
  15. }, false);

对于Go处理程序:

  1. func upload_handler(w http.ResponseWriter, r *http.Request, m render.Render) {
  2. w.Header().Set("Access-Control-Allow-Origin", "*")
  3. file, header, err := r.FormFile("C.Storage.FieldName")
  4. if err != nil {
  5. // 在这里抛出错误
  6. ServeHTTP(400, err.Error(), m)
  7. return
  8. }
  9. content, err := ioutil.ReadAll(file)
  10. if err != nil {
  11. ServeHTTP(400, err.Error(), m)
  12. return
  13. }
  14. fileSize, err := file.Seek(0, 2) //2 = from end
  15. if err != nil {
  16. ServeHTTP(400, err.Error(), m)
  17. return
  18. }
  19. if fileSize > int64(C.Storage.FileSize) {
  20. ServeHTTP(400, "File size limit exceeded", m)
  21. return
  22. }
  23. ftype := http.DetectContentType(content)
  24. if !strings.Contains(C.Storage.AllowedMimes, ftype) {
  25. ServeHTTP(400, "File type not allowed", m)
  26. return
  27. }
  28. // 上传到Amazon S3的函数
  29. file.Close()
  30. }

以上是要翻译的内容。

英文:

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 :

  1. document.getElementById("attachment").addEventListener("change", function(e) {
  2. var file = this.files[0];
  3. var xhr = new XMLHttpRequest();
  4. var formdata = new FormData();
  5. formdata.append("attachment", file);
  6. boundary=Math.random().toString().substr(2);
  7. xhr.onreadystatechange = function(e) {
  8. if ( 4 == this.readyState ) {
  9. console.log(["xhr upload complete", e]);
  10. }
  11. };
  12. xhr.open("POST", "https://upload_host:8443/upload", true);
  13. xhr.setRequestHeader("Content-Type","multipart/form-data; charset=utf-8; boundary=---------------------------"+boundary+";");
  14. xhr.send(formdata);
  15. }, false);

For the go handler :

  1. func upload_handler(w http.ResponseWriter, r *http.Request, m render.Render) {
  2. w.Header().Set("Access-Control-Allow-Origin", "*")
  3. file, header, err := r.FormFile("C.Storage.FieldName")
  4. if err != nil {
  5. // ERROR THROWN HERE
  6. ServeHTTP(400, err.Error(), m)
  7. return
  8. }
  9. content, err := ioutil.ReadAll(file)
  10. if err != nil {
  11. ServeHTTP(400, err.Error(), m)
  12. return
  13. }
  14. fileSize, err := file.Seek(0, 2) //2 = from end
  15. if err != nil {
  16. ServeHTTP(400, err.Error(), m)
  17. return
  18. }
  19. if fileSize > int64(C.Storage.FileSize) {
  20. ServeHTTP(400, "File size limit exceeded", m)
  21. return
  22. }
  23. ftype := http.DetectContentType(content)
  24. if !strings.Contains(C.Storage.AllowedMimes, ftype) {
  25. ServeHTTP(400, "File type not allowed", m)
  26. return
  27. }
  28. //FUNCTION TO UPLOAD TO AMAZON S3
  29. file.Close()
  30. }

答案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:

确定