javascript 创建 blob 期间 Internet Explorer 11 中的 InvalidStateError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29677339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:53:40  来源:igfitidea点击:

InvalidStateError in internet explorer 11 during blob creation

javascript

提问by Thushar P Sujir

I'm getting an InvalidStateErrorat the blob creation line on IE 11. Needless to say, it works in Chrome and Firefox. I can see that the binary data is my client side. Are there any alternatives to download this as a file?

InvalidStateError在 IE 11 上的 blob 创建行得到了一个。不用说,它适用于 Chrome 和 Firefox。我可以看到二进制数据是我的客户端。有没有其他方法可以将其下载为文件?

var request = new ActiveXObject("MicrosoftXMLHTTP");
request.open("post", strURL, true);
request.setRequestHeader("Content-type", "text/html");
addSecureTokenHeader(request);
request.responseType = 'blob';

request.onload = function(event) {
    if (request.status == 200) {
        var blob = new Blob([request.response], { type: 'application/pdf' });
        var url = URL.createObjectURL(blob);

        var link = document.querySelector('#sim');
        link.setAttribute('href', url);

        var filename =  request.getResponseHeader('Content-Disposition');
        $('#sim').attr("download", filename);
        $(link).trigger('click');
        fireEvent(link, 'click');
    } else {
        // handle error
    }
}

回答by Lars Anundsk?s

After instantiating an XmlHttpRequestwith xhr.responseType = "blob"I was getting an InvalidStateError. However, moving xhr.responseType = "blob"to onloadstartsolved it for me! :)

在实例化 an 之后XmlHttpRequestxhr.responseType = "blob"我得到了一个InvalidStateError. 然而,在移动xhr.responseType = "blob"onloadstart解决了这个问题对我来说!:)

xhr.onloadstart = function(ev) {
    xhr.responseType = "blob";
}

回答by Patrick Beaupérin

Spent some time on this and actually found out adding new Uint8Arrayworks:

花了一些时间,实际上发现了添加new Uint8Array作品:

var blob = new Blob([new Uint8Array(request.response)], {type: 'application/pdf'});

var blob = new Blob([new Uint8Array(request.response)], {type: 'application/pdf'});

回答by AndreiC

Is not a elegant way but it works on IE8 - IE11:

不是一种优雅的方式,但它适用于 IE8 - IE11:

var myForm = document.createElement("form");

myForm.method = "POST";
myForm.action = strURL;
myForm.target = "_blank";

var myInput = document.createElement("input");
myInput.type = "text";
myInput.name = "sim";
myInput.value = JSON.stringify(/*data to post goes here*/);
myForm.appendChild(myInput);

document.body.appendChild(myForm);
myForm.submit();
$(myForm).hide();

回答by Thomas Praxl

You need to use a BlobBuilder in that case.

在这种情况下,您需要使用 BlobBuilder。

From: https://github.com/bpampuch/pdfmake/issues/294#issuecomment-104029716

来自:https: //github.com/bpampuch/pdfmake/issues/294#issuecomment-104029716

try {
   blob = new Blob([result], { type: 'application/pdf' });
}
catch (e) {
   // Old browser, need to use blob builder
   window.BlobBuilder = window.BlobBuilder ||
                        window.WebKitBlobBuilder ||
                        window.MozBlobBuilder ||
                        window.MSBlobBuilder;
   if (window.BlobBuilder) {
       var bb = new BlobBuilder();
       bb.append(result);
       blob = bb.getBlob("application/pdf");
   }
}