XMLHttpRequest上传文件不适用于Microsoft Edge,但其他人

时间:2016-07-19 08:14:05

标签: javascript file-upload xmlhttprequest microsoft-edge

此java脚本代码适用于除Microsoft Edge之外的其他浏览器。调用send(file)方法后,不会发送请求。

this.uploadFile = function (file, index, filesStorage) {
        try {
            if (file == undefined || file == null) { return; }

            var xhr = new XMLHttpRequest();
            xhr.withCredentials = true;
            xhr.hasLoaded = false;

            // Update progress bar
            xhr.upload.addEventListener("loadstart", function (event) { filesManager_5.startFileProgress(event, file.uid); }, false);
            xhr.upload.addEventListener("progress", function (event) { filesManager_5.updateFileProgress(event, file.uid); }, false);
            xhr.upload.addEventListener("loadend", function (event) { filesManager_5.endFileProgress(event, file.uid); }, false);
            xhr.addEventListener("readystatechange", function (event) { filesManager_5.changeStateFileProgress(event, file.uid, this.readyState); }, false);
            xhr.upload.addEventListener("error", function (event) { alert('Error!'); }, false);
            //xhr.upload.addEventListener("abort", function (event) { alert('Abort!'); }, false);

            xhr.open("post", "~/Html5UploadHandler.ashx", true);

            xhr.setRequestHeader("Content-Type", file.type);
            xhr.setRequestHeader("X-File-Name", file.name);
            xhr.setRequestHeader("X-File-Type", file.type);
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            xhr.setRequestHeader('X-Upload-Id', GetUploadId());

            this._fileRequests[file.uid] = xhr;
            xhr.send(file);
        } catch (e) {
            alert(e);
        }
    };

1 个答案:

答案 0 :(得分:0)

我改成这个

之后就可以了
var reader = new FileReader(); 
reader.onload = function(){
    var arrayBuffer = this.result;

    xhr.send(arrayBuffer); 
}           
reader.readAsArrayBuffer(file);