Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  37] [ 2]  / answers: 1 / hits: 16813  / 9 Years ago, tue, february 3, 2015, 12:00:00

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



$scope.download = function() {
Restangular.one(myAPI)
.withHttpConfig({responseType: 'blob'}).customGET().then(function(response) {

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

//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)?


More From » angularjs

 Answers
4

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);
anchor.download = myfile.txt;
anchor.href = blobURL;
anchor.click();





Download attribute compatibility


[#67961] Monday, February 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maxinec

Total Points: 117
Total Questions: 116
Total Answers: 116

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
maxinec questions
;