问题描述
这适用于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用于创建和下载文件,称为msSaveBlob
或msSaveOrOpenBlob
。您应该使用其中之一来下载文件。 msSaveBlob
和msSaveOrOpenBlob
方法之间的区别在于,前者仅向用户提供 Save 按钮,而后者同时提供 Save 和打开按钮。
如果有Blob数据,则可以使用如下API:
if(window.navigator.msSaveOrOpenBlob) {
//IE11 & Edge,download function
window.navigator.msSaveOrOpenBlob(blob,fileName);
}
else{
//Other browsers,your download function
...
}
您也可以参考this thread和this thread了解更多信息。