我在html下面有锚标记用于下载文件。
<a [href]="myFileUrl"
class="u-text--document"
download="myfile.csv"><span>Title of the Excel document (6.8MB)</span></a>
这个方法我调用ngOninit(),
// subscribe to my file content
getMyFileTemplate(): any {
this.myService.getMyFile().subscribe((response) => {
const FileContent = response;
const blob = new Blob([FileContent], { type: 'application/octet-stream' });
if (navigator.appVersion.toString().indexOf('.NET') > 0) {
this.myFileUrl= window.navigator.msSaveBlob(blob, 'employees');
} else {
this.myFileUrl= this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));
}
});
}
}
这种方法在Google Chrome中运行良好,但在IE中,在页面重新加载时,它自动下载文件,甚至没有点击链接。有人可以帮忙解决这个问题吗?
在锚标记的click事件而不是ngOnInit()上调用此函数
<a [href]="myFileUrl" class="u-text--document" download="myfile.csv"(click)="getMyFileTemplate()">
<span>Title of the Excel document (6.8MB)</span>
</a>