出于某种原因,这在 IE 中似乎比 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;
});
};
有没有办法做类似于 IE10+ 的工作方式?也就是说,我可以指定一个文件名/类型(只会是 zip)?
最佳答案
一旦你有了你的对象 url,你就可以创建一个 anchor 并设置 download属性为您想要的文件名,将 href 设置为对象 url,然后只需调用 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
关于javascript - Angularjs/Restangular,如何命名文件 blob 以供下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28310366/