添加下载全部文件功能

This commit is contained in:
xixi 2024-10-29 10:28:55 +08:00
parent 9e7f0a2b7e
commit 606c710b73

View File

@ -69,63 +69,42 @@ export default function CardProj(props: any) {
const [tagIdArray, setTagIdArray] = useState(data.tagDataIds) const [tagIdArray, setTagIdArray] = useState(data.tagDataIds)
// 动态显示标签状态 // 动态显示标签状态
// const [tagStatus, setTagStatus] = useState('') // const [tagStatus, setTagStatus] = useState('')
// const downAll = (projId: string) => {
// get({ const downAll = (projId: string, name: string) => {
// messageApi,
// url: `${Axios.defaults?.baseURL}/route/proj/download/all/${projId}`,
// onBefore() {
// console.log('正在打包')
// },
// onSuccess(data: any) {
// console.log(data)
// },
// onFinally() {
// }
// })
// }
const downAll = async (projId: string, name: string) => {
setHasDown(true) setHasDown(true)
if (!hasDown) { if (!hasDown) {
get({
try { messageApi,
setDownModal(true) url: `/route/proj/download/all/${projId}`,
setHasDown(true) config: {
// 发送请求 responseType: 'blob' // 指定响应类型为 blob
const response = await Axios.get( },
`${Axios.defaults?.baseURL}/route/proj/download/all/${projId}`, onBefore() {
{ responseType: 'blob' } // 指定响应类型为 blob setDownModal(true); // 打开下载模态框
); setHasDown(true); // 设置正在下载状态
},
// 检查状态码 onSuccess(data:any) {
if (response.status === 200) { // 处理下载成功的逻辑
setDownModal(false) const blob = new Blob([data.data]); // 创建 Blob 对象
// 创建 Blob 对象 const url = window.URL.createObjectURL(blob); // 创建下载链接
const blob = new Blob([response.data], { type: response.headers['content-type'] }); const link = document.createElement('a'); // 创建临时的 <a> 元素
// 创建下载链接
const url = window.URL.createObjectURL(blob);
// 创建一个临时的 <a> 元素
const link = document.createElement('a');
link.href = url; link.href = url;
link.setAttribute('download', `${name}全部资料.zip`); // 设置下载文件的名称 link.setAttribute('download', `${name}全部资料.zip`); // 设置下载文件的名称
document.body.appendChild(link); document.body.appendChild(link);
link.click(); // 触发下载 link.click(); // 触发下载
document.body.removeChild(link); // 下载完成后移除 <a> 元素 document.body.removeChild(link); // 下载完成后移除 <a> 元素
// 释放 URL 对象
// 释放 URL 对象,建议在用户确认下载完成后解除
// window.URL.revokeObjectURL(url); // window.URL.revokeObjectURL(url);
} else { },
console.error('下载失败'); onFinally() {
// 在请求完成后(成功或失败)都会执行
setHasDown(false); // 重置 hasDown 状态
setDownModal(false); // 关闭模态框
} }
} catch (error) { })
console.error('请求失败', error);
} finally {
// 无论请求成功与否,下载完成后都重置 hasDown 状态
setHasDown(false);
setDownModal(false); // 关闭模态框
}
} else { } else {
setDownModal(true); setDownModal(true);
} }