vue 解决后端返回blob文件下载问题

                  function download (res) {  //res是后台返回的数据
                            let blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); //type为所需要下载的文件格式,以xlsx文件为例

                            let url = window.URL.createObjectURL(blob);
                            let fileName = "详情.xlsx";
                            if ("download" in document.createElement("a")) {
                                let a = document.createElement("a");
                                a.href = url;
                                a.download = fileName;
                                a.style.display = "none";
                                document.body.appendChild(a);
                                a.click();
                                URL.revokeObjectURL(a.href);
                                document.body.removeChild(a);
                            } else {
                                navigator.msSaveBlob(blob, fileName);
                            }
                        }

不同格式的文件对应的type :

文件类型 type
.doc application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.xls application/vnd.ms-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.txt text/plain
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。