【发布时间】:2017-08-31 07:12:46
【问题描述】:
如何在打字稿中为 blob 设置文件名?对于 IE,我可以轻松设置文件名,但对于 Chrome,这似乎是不可能的。基本上我需要类似于this solution 但带有打字稿的东西
downloadFile(data: any) {
var blob = new Blob([data], {type: 'text/csv'});
let fileName = 'my-test.csv';
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
//save file for IE
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
//save for other browsers: Chrome, Firefox
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}
}
这个函数是从 html UI/angular 2 调用的:
<button type="button" class="btn btn-success"
(click)="downloadFile('test')">Download <br /> Download </button>
【问题讨论】:
-
您是否尝试将
download属性添加到您的button? -
它不会与 hdownload 属性一起工作
标签: google-chrome angular typescript blob