2024-11-29 16:04:56 +08:00

71 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Message } from "element-ui";
const file = {
/** 下载文件
* @param data {blob} 文件流
* @param filename {String} 文件名称
*/
downloadFileByBlob: (data, filename) => {
if (!data) {
return false;
}
if (!!window.ActiveXObject || "ActiveXObject" in window) {
window.navigator.msSaveOrOpenBlob(new Blob([data]), filename);
} else {
const url = window.URL.createObjectURL(new Blob([data]));
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
},
/** 下载文件
* @param url {String} 文件下载地址
* @param filename {String} 文件名称
*/
downloadFileByUrl: (url, filename) => {
if (!!window.ActiveXObject || "ActiveXObject" in window) {
window.navigator.msSaveOrOpenBlob(url, filename);
} else {
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
},
dealBase64ToBlob(base64Url) {
// 截取base64的数据内容去掉前面的描述信息类似这样的一段data:image/png;base64,并解码为2进制数据
let bstr = atob(base64Url.split(",")[1]);
// 获取解码后的二进制数据的长度,用于后面创建二进制数据容器
let olength = bstr.length;
// 创建一个Uint8Array类型的数组以存放二进制数据
var u8arr = new Uint8Array(olength);
// 将二进制数据存入Uint8Array类型的数组中
while (olength--) {
u8arr[olength] = bstr.charCodeAt(olength);
}
// 创建blob对象
let blob = new Blob([u8arr]);
return blob;
},
// 获取文件扩展名
getFileExtension: (fileName) => {
const fileNameSplited = fileName.split(".");
return fileNameSplited[fileNameSplited.length - 1].toLowerCase();
},
getFileName: (fileName) => {
return filename.substring(0, filename.lastIndexOf("."));
},
};
export default file;