IE 11:发送多部分表单数据请求时出错:流意外结束 [英] IE 11: Error while sending Multipart Form Data request: Stream ended unexpectedly
问题描述
我正在尝试使用jQuery AJAX调用上传文件以及其他一些表单字段.
I am trying to upload files along with some other form fields using jQuery AJAX calls.
这是调用服务器上URL的常用功能:
This is a common function that calls the URL on the server:
function uploadDocument(rquestURL,formId,callback){
$.ajax({
type : 'POST',
url : rquestURL,
cache:false,
processData:false,
contentType:false,
data : new FormData($("#"+formId)[0])
}).done(function(response) {
callback(response);
});
}
通过浏览器的开发工具进行检查时,以下是各自的请求内容:
On examining from the dev tools from browsers, these are the respective request contents:
从IE11
-----------------------------7dfad39402e6
Content-Disposition: form-data; name="subject"
Test
-----------------------------7dfad39402e6
Content-Disposition: form-data; name="message"
Test test
-----------------------------7dfad39402e6
Content-Disposition: form-data; name="announcementAttachment"; filename=""
Content-Type: application/octet-stream
<Binary File Data Not Shown>
---------------------------7dfad39402e6
Chrome
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ
Content-Disposition: form-data; name="subject"
Test
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ
Content-Disposition: form-data; name="message"
Test test
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ
Content-Disposition: form-data; name="announcementAttachment"; filename=""
Content-Type: application/octet-stream
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ--
在服务器端,我们将请求解析为:
On server side, we are parsing the request as:
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload fileUpload = new ServletFileUpload(factory);
if (ServletFileUpload.isMultipartContent(request)) {
// get the request content and iterate through
items = fileUpload.parseRequest(request);
}
该代码在Chrome和Firefox上运行正常,但是当我从IE11尝试时会引发以下异常.
The code works fine from Chrome and Firefox, but throws the below exception when I tried from IE11.
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
我提到了这些SO问题,但徒劳无功.
I referred to these SO questions but in vain.
- Java-IE中的FormData抛出org.apache .commons.fileupload.MultipartStream $ MalformedStreamException:流意外结束
- Grails多部分服务流意外结束
- Java - FormData in IE throwing org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
- Grails Multipart Service Stream ended unexpectedly
任何有用的指针都值得赞赏.谢谢.
Any useful pointers are appreciated. Thanks.
推荐答案
发现了一个奇怪的问题.解决方法就是这样.
Turned out a weird issue. This is how it's resolved.
- 我们在表单末尾有一个复选框.当我们没有选择任何复选框时,就会发生提到的问题.该请求的格式不正确,因此服务器引发了错误.
- 在表单末尾添加了一个隐藏字段(确保这是最后一个表单字段),并为其分配了一些值.
就是这样.像魔术一样工作!
That' it. Worked like a magic!
更多信息这里.
这篇关于IE 11:发送多部分表单数据请求时出错:流意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!