常用工具函数
# 文件下载
/**
* @param: fileType - 文件类型
* @param: fileName - 文件名称
* @param: data - 数据流文件
**/
export function download(fileType, bucketName,data) {
let downType = "";
let downName = "";
if (fileType == "image") {
downType = "image/png";
downName = fileName + ".png";
} else if (fileType == "word") {
downType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
downName = fileName + ".docx";
} else if (fileType == "video") {
downType = "video/mpeg4";
downName = fileName + ".mp4";
} else if (fileType == "radio") {
downType = "audio/mpeg";
downName = fileName + ".mp3";
} else if (fileType == "pdf") {
downType = "application/pdf";
downName = fileName + ".pdf";
}
let blob = new Blob([data], { type: downType });
let downloadElement = document.createElement("a");
let href = window.URL.createObjectURL(blob);
downloadElement.href = href;
document.body.appendChild(downloadElement);
//downloadElement.setAttribute("download", downName);//设置下载名称
downloadElement.download = downName; //设置下载文件名称
downloadElement.click();
document.body.removeChild(downloadElement); //移除元素;防止连续点击创建多个a标签
window.URL.revokeObjectURL(href);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
编辑 (opens new window)
上次更新: 2022/11/20