Javascript 在 IE 中使用 jQuery.ajax 发送 multipart/formdata
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7899513/
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
Send multipart/formdata with jQuery.ajax in IE
提问by James Nine
Is there a way to do the following solution in Internet Explorer? (IE7 and up)
有没有办法在 Internet Explorer 中执行以下解决方案?(IE7 及以上)
link: Sending multipart/formdata with jQuery.ajax
链接:使用 jQuery.ajax 发送 multipart/formdata
The solution code works great in every browser but IE.
解决方案代码适用于除 IE 之外的所有浏览器。
采纳答案by Sarfraz
No, you can't use jQuery.ajax
to upload files and FormData
isn't supported by IE unfortunately.
不,您不能用于jQuery.ajax
上传文件,FormData
不幸的是 IE 不支持。
Check out the Uploadify jQuery pluginto upload files via ajax. You can also use jQuery Form Pluginto upload files via ajax.
查看Uploadify jQuery 插件以通过 ajax 上传文件。您还可以使用jQuery Form Plugin通过 ajax 上传文件。
回答by Annabelle
回答by Gkrish
I too have faced this problem ,it may be useful to some one in need . FormDatais supported only from IE10 onwards here's a link.Error because you cannot bind input fields in old browsers, like in a modern ones using FormData. You cannot upload files through AJAX in IE.Their are two alternative ways to do
我也遇到过这个问题,可能对有需要的人有用。 FormData仅从 IE10 开始支持,这里有一个链接。错误,因为您无法在旧浏览器中绑定输入字段,就像在使用 FormData 的现代浏览器中一样。你不能在 IE 中通过 AJAX 上传文件。他们有两种替代方法
Use some plugin like Uploadify jQuery plugin to upload files via
ajax. You can also use jQuery Form Plugin to
multiand uploadifyOther way is you may use iframe.
使用一些插件,如 Uploadify jQuery 插件,通过
ajax上传文件。您还可以使用 jQuery Form Plugin 进行
multi和uploadify另一种方式是您可以使用 iframe。
Here's is the code
这是代码
if(!isAjaxUploadSupported()){ //IE fallfack
var iframe = document.createElement("<iframe name='upload_iframe_myFile' id='upload_iframe_myFile'>");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("src","javascript:false;");
iframe.style.display = "none";
var form = document.createElement("form");
form.setAttribute("target", "upload_iframe_myFile");
form.setAttribute("action", "fileupload.aspx"); //change page to post
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");
form.style.display = "block";
var files = document.getElementById("myFile");//Upload Id
form.appendChild(files);
$conv("#container_myFile").html("Uploading...");
document.body.appendChild(form);
document.body.appendChild(iframe);
iframeIdmyFile = document.getElementById("upload_iframe_myFile");
// Add event...
var eventHandlermyFile = function () {
if (iframeIdmyFile.detachEvent)
iframeIdmyFile.detachEvent("onload", eventHandlermyFile);
else
iframeIdmyFile.removeEventListener("load", eventHandlermyFile, false);
response = getIframeContentJSON(iframeIdmyFile);
}
if (iframeIdmyFile.addEventListener)
iframeIdmyFile.addEventListener("load", eventHandlermyFile, true);
if (iframeIdmyFile.attachEvent)
iframeIdmyFile.attachEvent("onload", eventHandlermyFile);
form.submit();
return;
}
////var data = new FormData();
//// code go here(for modern browsers)
function isAjaxUploadSupported(){
var input = document.createElement("input");
input.type = "file";
return (
"multiple" in input &&
typeof File != "undefined" &&
typeof FormData != "undefined" &&
typeof (new XMLHttpRequest()).upload != "undefined" );
}
function getIframeContentJSON(iframe){
//IE may throw an "access is denied" error when attempting to access contentDocument on the iframe in some cases
try {
// iframe.contentWindow.document - for IE<7
var doc = iframe.contentDocument ? iframe.contentDocument: iframe.contentWindow.document,
response;
var innerHTML = doc.body.innerHTML;
//plain text response may be wrapped in <pre> tag
if (innerHTML.slice(0, 5).toLowerCase() == "<pre>" && innerHTML.slice(-6).toLowerCase() == "</pre>") {
innerHTML = doc.body.firstChild.firstChild.nodeValue;
}
response = eval("(" + innerHTML + ")");
} catch(err){
response = {success: false};
}
return response;
}
回答by idm
Try to set forms' attributes like this:
尝试像这样设置表单的属性:
$( "#yourformid" ) .attr( "enctype", "multipart/form-data" ) .attr( "encoding", "multipart/form-data" );
$( "#yourformid" ) .attr( "enctype", "multipart/form-data" ) .attr( "encoding", "multipart/form-data" );
or rather try to find ready-made jquery upload plugin
或者尝试寻找现成的 jquery 上传插件