主流浏览器下载后台返回的blob文件

直接上代码:

this._fileService.downLoadFile(data).subscribe((data: Blob)=> {
    console.log('queryAttachment',data);

    if ('msSaveOrOpenBlob' in navigator){ //IE10+时,走这里下载
        window.navigator.msSaveOrOpenBlob(data, '工商执照附件.jpeg');
    }else{  //其他主流浏览器走这里,利用a 标签的download属性下载

        let url = window.URL.createObjectURL(data);
        let link = document.createElement('a');

        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', '工商执照附件.jpeg');
        document.body.appendChild(link);
        link.click();
    }
},error => {
    console.log('queryAttachment---error',error);
});

你可能感兴趣的:(前端)