Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a blob from HTTP URL in IE 11

My page generates a URL like this: blob:http%3A//localhost%3A8383/568233a1-8b13-48b3-84d5-cca045ae384f, blob having file data. I am downloading this as a file in every browser except IE 11. How can I download this blob in IE 11? A new tab get open and continuous refreshing happen.

var file = new Blob([data], { type: 'application/octet-stream' });
var reader = new FileReader();
reader.onload = function (e) {
    var text = reader.result;
}
reader.readAsArrayBuffer(file);
var fileURL = URL.createObjectURL(file);
var filename = fileURL.replace(/^.*[\\\/]/, '');
var name = filename + '.doc';

var a = $("<a style='display: none;'/>");
a.attr("href", fileURL);
a.attr("download", name);
$("body").append(a);
a[0].click();
a.remove();
like image 271
Jeff Dean Avatar asked May 02 '16 09:05

Jeff Dean


People also ask

How do I open blob URL in ie11?

For inside iframe download in Internet Explorer 11, you need to use parent. window. navigator. msSaveOrOpenBlob(blob, "filename.

What is Blob HTTP URL?

The blob URI scheme, also known as an object URL, is a Uniform Resource Identifier (URI) scheme used for accessing locally generated data via APIs designed to work only with URLs.

How do I create a blob URL?

createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object. To release an object URL, call revokeObjectURL() .


1 Answers

IE11 Not support URL.createObjectURL()

Work for me.

IE11 I'm use

window.navigator.msSaveOrOpenBlob(blob, fileName);

Or, If check condition.

var blob = 'Blob Data';
if(window.navigator.msSaveOrOpenBlob) {

    // IE11
    window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {

    // Google chome, Firefox, ....
    var url = (window.URL || window.webkitURL).createObjectURL(blob);
    $('#filedownload').attr('download', fileName);
    $('#filedownload').attr('href', url);  
    $('#filedownload')[0].click();
}

Read more: Fixed URL.createObjectURL() function doesn't work in IE 11

Demo: JSFiddle

like image 164
bamossza Avatar answered Sep 20 '22 17:09

bamossza