常见的文件下载功能,我懒得以后再写了,以后就直接来cv,争取能早点下班,谁的时间不是时间的,善于做笔记,以后就可以省下很多时间。要是遇到非要我背诵出来的那种面试官,那我不去就是了。主打的就是一个好好做笔记,灵活使用,改改参数就行了。哈哈哈
import { request } from '@umijs/max';
// 文件下载
export async function fileDownload(params) {
const { fileName, id } = params;
return await request(`${host.filestorageUrl}/fileDownload/${id}`, {
method: 'POST',
body: { id },
responseType: 'blob',
customBlob: true,
transferFileName: fileName,
preview: false,
});
}
// 文件下载 fileDownload
const { run: fileDownloadRun, loading } = useRequest(fileDownload, {
manual: true,
onSuccess: (res, params) => {
try {
console.log(params);
download(res, res.type, params[0].fileName);
message.success('下载成功');
} catch (error) {
console.error('🚀 ~ file: index.tsx:23 ~ FileDownload ~ error:', error);
}
},
});
export function download(file, type, fileName = '未命名') {
console.log(file, type, fileName);
const blob = new Blob([file], { type });
const downloadElement = document.createElement('a');
const href = window.URL.createObjectURL(blob); // 创建下载的链接
downloadElement.href = href;
downloadElement.download = fileName; // 下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); // 点击下载
document.body.removeChild(downloadElement); // 下载完成移除元素
window.URL.revokeObjectURL(href); // 释放掉blob对象
}