文件导出
安装 file-saver
$ yarn add file-saver
1
设置 responseType
设置响应类型为blob
,这里使用的是 umi-request
export async function downloadTempExcel() {
return request("/refundFile/downloadTempExcel", {
responseType: "blob"
});
}
1
2
3
4
5
2
3
4
5
配置一些常用的格式类型
import { saveAs } from "file-saver";
export function saveAsExcel(data, fileName) {
const blobData = new Blob([data], {
type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blobData, fileName);
}
export function saveAsPPT(data, fileName) {
const blobData = new Blob([data], {
type:
"application/vnd.openxmlformats-officedocument.presentationml.presentation;charset=utf-8"
});
saveAs(blobData, fileName);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
图片下载
FileSaver.saveAs("https://httpbin.org/image", "image.jpg");
1
先检查服务器是否支持跨域请求,如果支持则使用 blob URLs 保存数据,如果不支持跨域则使用 a[download]