import axios, {Method} from 'axios'
export const exportFile = async (url: string, data: any, fileNames: string, method: string="POST") => {
const file = await axios({
method: method as Method,
url,
responseType: 'arraybuffer',
headers: {
token:data.token
},
data
});
let blob = new Blob([file.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, `${fileNames}`);
} else {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = e => {
const a = document.createElement('a');
a.download = fileNames;
a.href = e.target?.result as string;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
}
}
调用
export declare const exportFile: (url: string, data: any, fileNames: string, method?: string) => Promise<void>;
//转换为baseImg
let imageUrl ="data:image/png;base64," + btoa(new Uint8Array(data).reduce((data, byte) => data + String.fromCharCode(byte), ""));
导出文件功能支持ie
最新推荐文章于 2024-06-26 16:10:09 发布