Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidStateError in internet explorer 11 during blob creation

Tags:

javascript

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
    }
}
like image 633
Thushar P Sujir Avatar asked Apr 16 '15 14:04

Thushar P Sujir


1 Answers

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";
}
like image 101
Lars Anundskås Avatar answered Sep 23 '22 23:09

Lars Anundskås