pla*_*one 7 google-chrome blob typescript angular
如何在打字稿中设置blob的文件名?对于IE,我可以轻松设置文件名,但对于Chrome,它看起来不可能.基本上我需要类似于这个解决方案但有打字稿的东西
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);
}
}
Run Code Online (Sandbox Code Playgroud)
从html UI/angular 2调用此函数:
<button type="button" class="btn btn-success"
(click)="downloadFile('test')">Download <br /> Download </button>
Run Code Online (Sandbox Code Playgroud)
Pie*_*Duc 23
对于chrome(和firefox),你需要做一些工作来创建一个<a>元素并调用click:
downloadFile(data: any): void {
const blob: Blob = new Blob([data], {type: 'text/csv'});
const fileName: string = 'my-test.csv';
const objectUrl: string = URL.createObjectURL(blob);
const a: HTMLAnchorElement = document.createElement('a') as HTMLAnchorElement;
a.href = objectUrl;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(objectUrl);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7583 次 |
| 最近记录: |