时常可以用到的一些短小的函数,尽可能用ts去写。
下载文件
下载数据流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15function download(blob: Blob, filename: string) {
if ("download" in document.createElement("a")) {
const eleA = document.createElement("a");
eleA.download = filename;
eleA.style.display = "none";
eleA.href = URL.createObjectURL(blob);
document.body.appendChild(eleA);
eleA.click();
URL.revokeObjectURL(eleA.href); //释放URL对象
document.body.removeChild(eleA);
} else {
// IE下载
navigator.msSaveOrOpenBlob(blob, filename);
}
}下载指定文件(地址只想某个文件)
1
2
3
4
5
6export const download = (url: string) => {
const iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.src = url;
document.body.appendChild(iframe);
};
文件转 base64
1 | const getBase64 = (file: Blob) => { |