1

According to the post here

I can download a variable(lets say a form filled by the user) in js to a File in Browser (I am aware that is not the way to go, and that the server must do that for my request)

I am using this code:

textToSave = "Hello"
var createObjectURL = (window.URL || window.webkitURL || {}).createObjectURL || function(){}; 
    var blob = null;
    var content = textToSave;
    var mimeString = "application/octet-stream"; 
    window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;  

    if(window.BlobBuilder){
        var bb = new BlobBuilder();
        bb.append(content);
        blob = bb.getBlob(mimeString);
    }else{
        blob = new Blob([content], {type : mimeString});
    }
    var url = createObjectURL(blob);
    var a = document.createElement("a");
    a.href = url
    a.download = "file.csv";
    a.innerHTML = "download file";
    a.click();

and is working fine in Chrome, but when I try in IE-11 then I get a BLOB

enter image description here

Do you have an Idea how to make it work browser independent?

Community
  • 1
  • 1

1 Answers1

1

Don't worry about using object URLs - they are not fully supported in IE anyway. Would recommend using FileSaver.js, and then once you've got your blob the following should work for just about any relevant browser:

function saveBlob(){
    var theBlob = //... your stuff here
    saveAs(blob, 'file.csv');
}

And call it from your anchor:

<a href="#" onclick="saveBlob();">download file</a>
AndySis
  • 11
  • 3