在重新加载页面时,文件正在Internet Explorer中自动下载。如何解决这个问题?

问题描述 投票:0回答:1

我在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中,在页面重新加载时,它自动下载文件,甚至没有点击链接。有人可以帮忙解决这个问题吗?

javascript angular angular5 angular2-forms
1个回答
0
投票

在锚标记的click事件而不是ngOnInit()上调用此函数

<a [href]="myFileUrl"  class="u-text--document" download="myfile.csv"(click)="getMyFileTemplate()">
    <span>Title of the Excel document (6.8MB)</span>
</a>
© www.soinside.com 2019 - 2024. All rights reserved.