Angularjs/Restangular,如何命名文件blob以供下载?

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

出于某种原因,这在 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+ 工作方式的事情?也就是说,我可以指定一个文件name/type(只会是zip)?

一旦你有了你的对象 url 你就可以创建一个锚点并将 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

只需使用https://www.npmjs.com/package/angular-file-saver 浏览器支持 table 可以在这里看到:https://github.com/eligrey/FileSaver.js/