1 let that = this
2 ({
3 url: 'xxxxxx',
4 method: 'get',
5 data:{},
6 responseType: 'blob', // 后台返回的数据会被强制转为blob类型
7 }).then(res => {
8 let reader = new FileReader();
9 (res)
10 = function (result) {
11 try {
12 let resData = (); // 解析对象成功,说明是json数据
13 if () {
14 that.$message({
15 type: 'error',
16 message:
17 })
18 }
19 } catch (err) { // 解析成对象失败,说明是正常的文件流
20 let blob = new Blob([res], {type: "application/-excel"});
21 var link = ('a');
22 = (blob);
23 = `文件名.xls`;
24 ()
25 }
26 };
27 })
1. 数据异常,后台返回 blob 类型异常信息:
使用 new FileReader(),将 blob 转为 json,然后进行处理
2. 数据正常,后台返回 blob 文件流:
通过方法,直接下载