Search code examples
angulargoogle-chromeinternet-explorerfirefoxblob

(Angular) Need help to download a cvs file in IE, it works on Chrome and Firefox


This works on chrome and firefox, but i need this in IE, i don't know how to make it works. Internet Explorer console says "access denied"

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);
      }
    );
  }

Solution

  • I don't know how you download the csv file as you don't show it in your example code. But I think the error is due to you're using some methods not supported by IE.

    IE has its own API for creating and downloading files, which is called msSaveBlob or msSaveOrOpenBlob. You should use one of them to download files. The difference between the msSaveBlob and msSaveOrOpenBlob methods is that the former only provides a Save button to the user whereas the latter provides both a Save and an Open button.

    If you have the blob data, you can use the API like this:

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

    You can also refer to this thread and this thread for more information.