角度需要帮助才能在IE中下载cvs文件,该文件可在Chrome和Firefox上运行

问题描述

这适用于chrome和firefox,但我在IE中需要它,我不知道 如何使其运作。 Internet Explorer控制台显示“访问被拒绝”

descargarPlantilla() {
    this.cargaMasivaService.generarCSVPLD().subscribe(
      data => {
        const blob = new Blob([data._body],{ type: 'application/octet-stream' });
        const contentdisposition = data.headers.get('content-disposition');
        if (contentdisposition && contentdisposition.indexOf('attachment') !== -1) {
          const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
          const matches = filenameRegex.exec(contentdisposition);
        }
        this.modalMensajeService.moDalexito('El Documento se ha descargado exitosamente');
      },error => {
        this.modalMensajeService.moDalerror(error);
      }
    );
  }

解决方法

我不知道您如何下载csv文件,因为您没有在示例代码中显示它。但是我认为该错误是由于您使用的是IE不支持的某些方法引起的。

IE具有its own API用于创建和下载文件,称为msSaveBlobmsSaveOrOpenBlob。您应该使用其中之一来下载文件。 msSaveBlobmsSaveOrOpenBlob方法之间的区别在于,前者仅向用户提供 Save 按钮,而后者同时提供 Save 打开按钮。

如果有Blob数据,则可以使用如下API:

if(window.navigator.msSaveOrOpenBlob) {
    //IE11 & Edge,download function
    window.navigator.msSaveOrOpenBlob(blob,fileName); 
}
else{
   //Other browsers,your download function        
    ...
}

您也可以参考this threadthis thread了解更多信息。