Open links made by createObjectURL in IE11 Open links made by createObjectURL in IE11 javascript javascript

Open links made by createObjectURL in IE11


This demo uses Blob URL which is not supported by IE due to security restrictions.

IE has its own API for creating and downloading files, which is called msSaveOrOpenBlob.

Here is my cross-browser solution that works on IE, Chrome and Firefox:

function createDownloadLink(anchorSelector, str, fileName){    if(window.navigator.msSaveOrOpenBlob) {        var fileData = [str];        blobObject = new Blob(fileData);        $(anchorSelector).click(function(){            window.navigator.msSaveOrOpenBlob(blobObject, fileName);        });    } else {        var url = "data:text/plain;charset=utf-8," + encodeURIComponent(str);        $(anchorSelector).attr("download", fileName);        $(anchorSelector).attr("href", url);    }}$(function () {    var str = "hi,file";    createDownloadLink("#export", str, "file.txt");});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><a id="export" class="myButton" download="" href="#">export</a>