Uploaded file only contains "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--

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:

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

Controller:

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

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

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.

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")
   }
}
链接地址: http://www.djcxy.com/p/80478.html

上一篇: 使用模板内的条件

下一篇: 上传的文件只包含“WebKitFormBoundary”