scala 上传的文件只包含“WebKitFormBoundary”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24769832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:24:39  来源:igfitidea点击:

Uploaded file only contains "WebKitFormBoundary"

javascriptjqueryscalaplayframeworkplayframework-2.0

提问by bad at scala

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

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

------WebKitFormBoundaryJ0uWMNv89fcUsC1t--

------WebKitFormBoundaryJ0uWMNv89fcUsC1t--

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.

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

Form:

形式:

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

Javascript:

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

Controller:

控制器:

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

采纳答案by bad at scala

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

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

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

However, I also had problems with parse.temporaryFileand 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.

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

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

为了解决这个问题,我只是按照Play 文档使用了默认的 multipartform 上传方法,它运行良好。

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")
   }
}