使用jQuery发送multipart/formdata.ajax在IE中
Send multipart/formdata with jQuery.ajax in IE
是否有办法在Internet Explorer中实现以下解决方案?(IE7及以上版本)
链接:用jQuery.ajax发送multipart/formdata
解决方案代码在除IE以外的所有浏览器中都能很好地工作。
不可以,你不能使用jQuery.ajax
上传文件,不幸的是,IE不支持FormData
。
查看Uploadify jQuery插件通过ajax上传文件。你也可以使用jQuery表单插件通过ajax上传文件
遗憾的是,IE还不支持FormData
API。但是,您可以使用任意数量的jQuery AJAX表单post插件来实现类似的功能,例如这个。
我也遇到过这个问题,它可能对需要帮助的人有用。FormData仅在IE10以后才被支持,这里有一个链接。错误,因为你不能在旧的浏览器中绑定输入字段,就像在使用FormData的现代浏览器中。在IE中不能通过AJAX上传文件。有两种方法可以做
-
使用一些插件,如Uploadify jQuery插件通过
上传文件ajax。你也可以使用jQuery表单插件
多和uploadify
下面是代码
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;
}
尝试这样设置表单的属性:
$("#yourformid") .attr("enctype", "multipart/form-data") .attr("encoding", "multipart/form-data");
或者尝试找到现成的jquery上传插件
相关文章:
- Jquery Modal表单登录与AJAX-ASP经典上的IE 9
- IE出现奇怪的ajax问题
- IE上的新XMLHttpRequest()出现JS Ajax未指定错误
- 对于Ajax调用,为什么innerHTML没有显示在IE中
- 当在coldfusion中通过AJAX传递时,FORM提交在IE浏览器中给出空表单
- jQuery AJAX 上传系统 IE 问题
- IE 9、10、11-Ajax调用未返回
- $.get-Ajax中的alert()在IE中不工作
- jQuery日期选择器在Firefox和IE的MVC部分视图上进行ajax更新后无法工作
- IE 11出现奇怪的AJAX错误
- Ajax加载程序gif在ie和chrome中不起作用
- 当尝试仅在 IE 上使用 ajax 请求读取本地 xml 文件时,访问将被拒绝
- IE 焦点事件在 ajax 加载后随机失败
- IE 无法将响应标头(来自 ajax)打印为 utf8
- IE 的行为与 jQuery change/ajax 事件不一致
- 在 Jquery Ajax IE 之后访问 getElementsByName 数组
- 连续的 ajax 调用会冻结 chrome 和 IE
- jQuery AJAX 调用在 Firefox 中有效,但在 IE 中不起作用 - 返回有效响应
- jQuery AJAX IE - skipped functions ajax
- Ajax:IE上的空白