Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm getting an InvalidStateError at 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?

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
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.5k views
Welcome To Ask or Share your Answers For Others

1 Answer

After instantiating an XmlHttpRequest with xhr.responseType = "blob" I was getting an InvalidStateError. However, moving xhr.responseType = "blob" to onloadstart solved it for me! :)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...