Java 使用 jQuery Ajax 上传文件时出现“未找到多部分边界”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20609459/
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-08-13 03:06:08  来源:igfitidea点击:

'no multipart boundary was found' exception while uploading file using jQuery Ajax

javajqueryjspservletsfile-upload

提问by Abhishek Singh

Here is my jQuery Snippet

这是我的 jQuery 片段

$("#uploadForm").submit(function (e) {
    $.ajax({
        url: 'uploadExcel',
        data: $('#uploadForm').serialize(),
        cache: false,
        contentType: 'multipart/form-data',
        processData: false,
        type: 'POST',
        success: function (data) {
            alert(data);
        }
    });

while uploading the file i get the following error

上传文件时出现以下错误

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:931)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
    at com.obs.controller.ExcelUploadController.doPost(ExcelUploadController.java:36)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

What is the reason ? How it can be overcome ?

是什么原因 ?如何克服?

采纳答案by Lasse

The reason it that XMLHttpRequest1 doesn't support file upload, see this SO question: jQuery Ajax File Upload. You're getting the error on the server-side because you're telling the server to expect a multipart-upload but since no payload (= data) is sent with it, an error is thrown.

它之所以是XMLHttpRequest1不支持文件上传,请参阅本SO问题:jQuery的阿贾克斯文件上传。您在服务器端收到错误,因为您告诉服务器期待分段上传,但由于没有随它发送有效负载(= 数据),因此会引发错误。

You'll need FormDatafrom XMLHttpRequest2 (Beware:Only IE10 and up. All other browsers already support it. See Can I Use?for detailed support info).

您需要FormDataXMLHttpRequest2 开始(注意:仅 IE10 及更高版本。所有其他浏览器都已支持它。有关详细支持信息,请参阅我可以使用吗?)。

It seems FormDatacan be emulated in older browsers, though I havn't personally tried.

虽然我没有亲自尝试过,但它似乎FormData可以在较旧的浏览器中进行模拟

With XMLHttpRequest 2and FormData, your code would work like this:

使用XMLHttpRequest 2and FormData,您的代码将像这样工作:

$("#uploadForm").submit(function (e) {
    e.preventDefault();
    $.ajax({
        url: 'upload.ajax.php',
        data: new FormData($(this)[0]),
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function (data) {
           console.log(data);
        }
    }); 
});

回答by André Luis

For me, the problem was to wrap the image on form/input-file. I am new in javascript/Jquery, so that was really painful. I know that my server side was ok because I wrote a simple html form and it worked well. I was doing this new page with javascript to preview the image and I got that 'no multipart boundary was found' exception.

对我来说,问题是将图像包装在表单/输入文件上。我是 javascript/Jquery 的新手,所以这真的很痛苦。我知道我的服务器端没问题,因为我写了一个简单的 html 表单并且运行良好。我正在用 javascript 做这个新页面来预览图像,但我得到了“未找到多部分边界”的异常。

I found the answer for this herein StackOverflow, thanks to chandoo.

我找到了答案此这里在StackOverflow上,这要归功于chandoo

Try this: if the id of the form is "upload_form".

试试这个:如果表单的 id 是“upload_form”。

var formData = new FormData($('#upload_form')[0]);
formData.append('tax_file', $('input[type=file]')[0].files[0]);

$.ajax({
    url: 'uploadExcel',
    data: formData,
    cache: false,
    contentType: false, //using 'multipart/form-data' didn't work for me
    processData: false, //this is also important when you are dealing with files.
    type: 'POST',
    success: function (data) {
        alert(data);
    }
});

Good look!

好看!