WBC*_*WBC 9 javascript angularjs restangular
由于某种原因,这在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;
});
};
Run Code Online (Sandbox Code Playgroud)
有没有办法做类似于IE10 +的工作方式?也就是说,我可以指定一个文件名/类型(只会是zip)?
Pat*_*ans 27
只要有了对象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();Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15648 次 |
| 最近记录: |