reading an object URL produced in a worker with IE

2019-08-14 05:47发布

问题:

I have a webworker that is producing a CSV for downloading, and to conserve memory I only have it return the URL it produces from teh blob.. My worker code looks something like::

var blob = new Blob([ResultOfSomeWork()],{type:'text/csv'});
URL.createObjectURL(blob);
self.postMessage({url:blob.url});

My goal is to just be able to download it in firefox and chrome this is very easy as I can just set up an invisible <a> and have it be clicked to download it.

For IE10 I want to use msSaveBlob but I need a blob which I don't want to transfer. How can I download a object dataurl in IE10?

回答1:

So I found a solution that works. Apparently, I can XHR and read the content back in my main thread.

worker.onmessage = function(event){
    var url = event.data.url;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType='blob';
    xhr.onload = function(){
       msSaveBlob(xhr.response, fileName +'.csv');
    };
    xhr.send();
}

While this feels extremely complicated it works well in practice and is really fast.