上传的文件仅包含“WebKitFormBoundary" [英] Uploaded file only contains "WebKitFormBoundary"

查看:18
本文介绍了上传的文件仅包含“WebKitFormBoundary"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道这里发生了什么.每次我尝试上传文件时,所有文件都包含:

I don't really know what's going on here. Every time I try to upload a file, all the file contains is:

------WebKitFormBoundaryJ0uWMNv89fcUsC1t--

过去 2 天我一直在寻找某种解释,但我只是在兜兜转转.我不知道为什么会这样.

I have searched for the past 2 days for some sort of explanation, but I am just going in circles. I have no idea why this is happening.

<form id="upload-file" ecntype="multipart/form-data">
    <input name="picture" type="file">
    <input type="button" value="Upload" id="upload-button" />
</form>

Javascript:

$('#upload-button').click(function(e){
        e.preventDefault();
        var formData = new FormData($('#upload-file'));
        $.ajax({
            url: '/image',  
            type: 'POST',
            xhr: function() {  
                var myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ 
                    myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
                }
                return myXhr;
            },
            data: formData,
            cache: false,
           // contentType: false,
            processData: false
        });
    });

控制器:

def image = Action(parse.temporaryFile) { request =>
   request.body.moveTo(new File("/tmp/picture"))
   Ok("File uploaded")
}

推荐答案

问题出在 Javascript 中,而不是 Scala 中.我没有错误地引用表单元素.

The problem was occuring in the Javascript, not the Scala. I was not referencing the form elements improperly.

var formData = new FormData($('#upload-file')[0]);

但是,我在使用 parse.temporaryFile 时也遇到了问题,它没有使用上面的代码正确存储文件.当我在文本编辑器中检查存储的文件时,我注意到文件开头仍然有 ------WebKitFormBoundaryJ0uWMNv89fcUsC1t-- 内容,然后是表单信息,然后是文件字节.

However, I also had problems with parse.temporaryFile and it was not properly storing the file using the code above. When I inspected the stored files in a text editor, I noticed it still had the ------WebKitFormBoundaryJ0uWMNv89fcUsC1t-- stuff at the beginning of the file, followed by the form information, then followed by the file bytes.

为了解决这个问题,我只是根据 Play 文档使用了默认的多部分上传方法,而且效果很好.

To fix this, I just used the default method for multipartform upload as per the Play Documentation, and it worked perfectly.

def image = Action(parse.multipartFormData)  { request =>
   request.body.file("picture").map { picture =>
      val filename = picture.filename
      picture.ref.moveTo(new File(s"/tmp/picture/$filename"))
      Ok("ok")
   }.getOrElse {
      InternalServerError("file upload error")
   }
}

这篇关于上传的文件仅包含“WebKitFormBoundary"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆