上传的文件仅包含“WebKitFormBoundary"

编程入门 行业动态 更新时间:2024-10-27 02:30:21
本文介绍了上传的文件仅包含“WebKitFormBoundary"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 09:03:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1039120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   文件   quot   WebKitFormBoundary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!