简体   繁体   English

XMLHttpRequest文件上传在IE11中不起作用

[英]XMLHttpRequest file upload not working in IE11

Hi I have the following JS on my page. 嗨,我的页面上有以下JS。 It is working fine on chrome and firefox. 在chrome和firefox上运行正常。 but its not working on Internet Explorer 11. I'm a salesforce developer and I don't know much javascript. 但它不能在Internet Explorer 11上使用。我是一名Salesforce开发人员,并且对JavaScript的了解不多。 Can you please help me find where the issue is? 您能帮我找到问题所在吗? Thanks in advance. 提前致谢。

 tests = { filereader: typeof FileReader != 'undefined', dnd: 'draggable' in document.createElement('span'), formdata: !!window.FormData, progress: "upload" in new XMLHttpRequest }, support = { filereader: document.getElementById('filereader'), formdata: document.getElementById('formdata'), progress: document.getElementById('progress') }, progress = document.getElementById('uploadprogress'), fileupload = document.getElementById('upload'); "filereader formdata progress".split(' ').forEach( function (api) { if (tests[api] === false) { support[api].className = 'fail'; } else { support[api].className = 'hidden'; } } ); function textBeforeDrag(flag){ if(flag) { holder_txt1.className = ''; holder_txt2.className = 'hidden'; }else{ holder_txt1.className = 'hidden'; holder_txt2.className = ''; } } function resetAll() { holder.className = holder_txt1.className = ''; holder_txt2.className = uploadStatus.className = 'hidden'; } function readfiles(files) { var goodSize = true; var formData = tests.formdata ? new FormData() : null; for (var i = 0; i < files.length; i++) { size = typeof ActiveXObject !== 'undefined' ? getIEFileSize(files[i]) : files[i].fileSize || files[i].size; goodSize = 5000000 > size; if(!goodSize) { alert(files[i].name +' is too large, please choose a file that is 5Mb or less'); return; } goodSize = 26214400 > size+{!allAttSize}; if(!goodSize) { alert(this.files[0].name +' is too large - Total Attachment Size should not exceed 25MB'); return; } uploadStatus.className = ''; holder.className = 'hidden'; // now post a new XHR request if (tests.formdata) { var xhr = new XMLHttpRequest(); var sfdcurl = 'https://'+sfdcHostName+'.salesforce.com/services/apexrest/DragAndDrop/v1?FileName='+encodeURIComponent(files[i].name)+'&cType='+encodeURIComponent(files[i].type)+ '&parId={!thisCase.id}'; xhr.open('POST','/services/proxy' ); xhr.setRequestHeader("Authorization","Bearer {!$Api.Session_ID}"); xhr.setRequestHeader('SalesforceProxy-Endpoint', sfdcurl); xhr.setRequestHeader('X-User-Agent', 'DragAndDropAPI v1.0'); xhr.onload = function() { progress.value = progress.innerHTML = 100; }; if (tests.progress) { xhr.upload.onprogress = function (event) { if (event.lengthComputable) { var complete = (event.loaded / event.total * 100 | 0); progress.value = progress.innerHTML = complete; } } } xhr.onreadystatechange=function() { if (xhr.readyState==4 && xhr.status != 200) { if(xhr.responseText) alert(xhr.responseText); else alert('Some error occurred while uploading file'); console.log(xhr); }else{ } } xhr.send(files[i]); } } setTimeout(function(){addDroppedAttachment();},1000); } if (tests.dnd) { holder.ondragover = function () { this.className = 'hover'; textBeforeDrag(false); return false; }; holder.ondragend = function () { this.className = ''; textBeforeDrag(true); return false; }; holder.ondrop = function (e) { textBeforeDrag(true); this.className = ''; e.preventDefault(); readfiles(e.dataTransfer.files); resetAll(); } } else { fileupload.className = 'hidden'; fileupload.querySelector('input').onchange = function () { readfiles(this.files); }; } 

In the line : 在行中

var xhr = new XMLHttpRequest();

Change for : 更改为

var xhr = new ActiveXObject('Msxml2.XMLHTTP');

Now, your file upload work only in Internet Explorer 11. 现在,您的文件上传在Internet Explorer 11中有效。

Maybe, for you file upload to work in all navigator's (that support XMLHTTPRequest), try this: 也许,要使文件上传在所有导航器 (支持XMLHTTPRequest) 中都能正常工作 ,请尝试以下操作:

if('ActiveXObject' in window){
   return new ActiveXObject('Msxml2.XMLHTTP');
}else{
   return new XMLHttpRequest();
}

Source: http://www.purplesquirrels.com.au/2014/06/local-ajax-calls-ie11/ . 来源: http : //www.purplesquirrels.com.au/2014/06/local-ajax-calls-ie11/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM