Angularjs/Restangular, how to name file blob for download?


For some reason this seems easier in IE than Chrome/FF:

$ = function() {
      .withHttpConfig({responseType: 'blob'}).customGET().then(function(response) {

        //IE10 opens save/open dialog with
        window.navigator.msSaveOrOpenBlob(response, '');

        //Chrome/FF downloads a file with random name                
        var url = (window.URL || window.webkitURL).createObjectURL(response);
        window.location.href = url;

Is there a way to do something similar to how IE10+ works? That is, I can specify a file name/type (will only be zip)?


As soon as you have your object url you can create an anchor and set the download attribute to the filename you want, set the href to the object url, and then just call click

var myBlob = new Blob(["example"],{type:'text/html'})
var blobURL = (window.URL || window.webkitURL).createObjectURL(myBlob);
var anchor = document.createElement("a"); = "myfile.txt";
anchor.href = blobURL;;

Download attribute compatibility

Answered By – Patrick Evans

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.