![](/img/trans.png)
[英]multipart/formdata is not sending file data with jQuery.ajax
[英]Send multipart/formdata with jQuery.ajax in IE
有沒有辦法在Internet Explorer中執行以下解決方案? (IE7及更高版本)
鏈接: 使用jQuery.ajax發送multipart / formdata
該解決方案代碼在IE之外的所有瀏覽器中都可以正常工作。
不,您不能使用jQuery.ajax
上傳文件,不幸的是IE不支持FormData
。
請查看Uploadify jQuery插件,以通過ajax上傳文件。 您還可以使用jQuery Form Plugin通過ajax上傳文件。
不幸的是,IE還不支持FormData
API。 但是,您可以使用任意數量的jQuery AJAX表單發布插件來實現類似功能,例如this 。
我也曾經遇到過這個問題,它可能對某些需要幫助的人有用。 僅從IE10開始才支持FormData,這是一個鏈接 。錯誤,因為您不能像在使用FormData的現代瀏覽器中那樣綁定舊瀏覽器中的輸入字段。 您無法通過IE中的AJAX上傳文件,這是兩種替代方法
這是代碼
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上傳插件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.