XMLHttpRequest文件上传在IE11中无效

时间:2016-07-26 12:48:44

标签: javascript internet-explorer file-upload xmlhttprequest

您好,我的页面上有以下JS。它在chrome和firefox上运行良好。但它不适用于Internet Explorer 11.我是一名salesforce开发人员,我不太了解javascript。你能帮我找一下问题所在吗? 提前谢谢。

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);
        };
    }

1 个答案:

答案 0 :(得分:2)

行中
var xhr = new XMLHttpRequest();

更改

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

现在,您的文件上传仅在Internet Explorer 11中

也许,对于您的文件上传适用于所有导航器(支持XMLHTTPRequest),请尝试以下操作:

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

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