【问题标题】:(Angular) Need help to download a cvs file in IE, it works on Chrome and Firefox(Angular) 需要帮助在 IE 中下载 csv 文件,它适用于 Chrome 和 Firefox
【发布时间】:2020-12-01 08:32:21
【问题描述】:

这适用于 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);
      }
    );
  }

【问题讨论】:

    标签: angular google-chrome internet-explorer firefox blob


    【解决方案1】:

    我不知道您是如何下载 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 了解更多信息。

    【讨论】:

    • @JAVMTZ 如果答案有助于解决您的问题,您可以参考this。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解。
    猜你喜欢
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多