使用AJAX在IE9其作品发送文件/文件上传 [英] Sending files /file upload using ajax which works in IE9
问题描述
我要上传使用AJAX已在IE9要支持的文件。我用FORMDATA提到<一个href="http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax">here.我的code是这样的:
I need to upload files using ajax which has to be supported in IE9. I was using FormData as mentioned here. My code looks like this:
var files = new FormData();
JQuery.each($('#file')[0].files, function (i, file) {
files.append('file', file);
});
$.ajax({
type: "POST",
url: '/url',
cache: false,
contentType: false,
processData: false,
data: files,
...
});
这正常工作在Safari和Firefox,但未能在IE9的FORMDATA不支持IE9。我试图通过设置发送就像一个文件:
This works fine in Safari and Firefox, but fails in IE9 as the FormData is not supported in IE9. I tried sending just as a file by setting:
data: $('#file')[0].files[0]
contentType: 'multipart/form-data'
这失败的数据被发送URL恩codeD的形式,并不能在Java方面进行分析。任何帮助或指针就如何解决这将是极大的AP preciated。我需要的东西,在所有浏览器上运行。
This fails as the data is sent in url-encoded form and is cannot be parsed at the java side. Any help or pointer on how to solve this will be greatly appreciated. I need something that works across all browsers.
编辑:我不需要任何的上传进度条的文件通常很小。我并不需要上传多个文件。我只是需要一个单一的文件上传。
I do not need any upload progress bar as the files are usually small. I do not need to upload multiple files. I just need a single file upload.
推荐答案
不幸的是,你不能使用Ajax( XMLHtt prequest
换句话说)发送文件,但您可以通过实施类似的行为&LT; IFRAME /&GT;
与&LT;形式方法=邮报ENCTYPE =多部分/形式 - 数据/&GT;
包含一个&LT;输入类型=文件/&GT;
这将使用用户选择的文件自然方法。您可以使用JavaScript调用 form.submit()
再查询该&LT; IFRAME /&GT;
从父文档检查文件上传过程是否已经完成。
Unfortunately you cannot use Ajax (XMLHttpRequest
in other words) for sending files, but you can implement a similar behavior using the <iframe/>
with a <form method="post" enctype="multipart/form-data"/>
that contains an <input type="file"/>
which sends a user chosen file using the "natural" way. You can use javascript to call the form.submit()
then poll that <iframe/>
from parent document to check whether the file upload process is done.
的jQuery
拥有的很多很酷的插件,以得到这个工作做好,有我最喜欢的,例如。
jQuery
has a lot of cool plugins for getting this job done, there is my favorite one, for example.
这篇关于使用AJAX在IE9其作品发送文件/文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!