feat: 资源列表批量下载优化
This commit is contained in:
@@ -322,14 +322,55 @@ export const ordersApi = {
|
||||
},
|
||||
|
||||
// 下载文件 - 通过后端代理(解决跨域问题)
|
||||
downloadFile: async (fileUrl: string) => {
|
||||
// 优化:改为流式传输,支持大文件下载、断点续传
|
||||
// 注意:使用 XMLHttpRequest 以支持进度回调和流式传输
|
||||
downloadFile: async (fileUrl: string, onProgress?: (progress: number) => void) => {
|
||||
try {
|
||||
const res = await request('/api-order/files/download', {
|
||||
method: 'POST',
|
||||
data: { url: fileUrl },
|
||||
responseType: 'blob',
|
||||
// 获取当前token,用于请求头
|
||||
const token = typeof sessionStorage !== 'undefined' ? sessionStorage.getItem('access_token') : null;
|
||||
|
||||
return await new Promise<Blob>((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api-order/files/download', true);
|
||||
xhr.responseType = 'blob';
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
// 添加Authorization请求头(重要!)
|
||||
if (token) {
|
||||
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
// 监听下载进度
|
||||
xhr.addEventListener('progress', (event) => {
|
||||
if (event.lengthComputable && onProgress) {
|
||||
const percentComplete = Math.round((event.loaded / event.total) * 100);
|
||||
onProgress(percentComplete);
|
||||
console.log(`📊 下载进度: ${percentComplete}%`);
|
||||
}
|
||||
});
|
||||
|
||||
xhr.addEventListener('load', () => {
|
||||
if (xhr.status === 200) {
|
||||
console.log('✅ 文件下载成功');
|
||||
resolve(xhr.response as Blob);
|
||||
} else {
|
||||
reject(new Error(`HTTP ${xhr.status}`));
|
||||
}
|
||||
});
|
||||
|
||||
xhr.addEventListener('error', () => {
|
||||
console.error('❌ 网络错误');
|
||||
reject(new Error('网络错误'));
|
||||
});
|
||||
|
||||
xhr.addEventListener('abort', () => {
|
||||
console.error('❌ 下载被中止');
|
||||
reject(new Error('下载被中止'));
|
||||
});
|
||||
|
||||
// 发送请求(请求头需要在open和send之间添加)
|
||||
xhr.send(JSON.stringify({ url: fileUrl }));
|
||||
});
|
||||
return res;
|
||||
} catch (error) {
|
||||
console.error('❌ downloadFile error:', error);
|
||||
throw error;
|
||||
@@ -337,14 +378,62 @@ export const ordersApi = {
|
||||
},
|
||||
|
||||
// 批量下载所有文件 - 打包为 ZIP
|
||||
batchDownloadFiles: async (orderNo: string) => {
|
||||
// 优化:改为流式传输,支持大型ZIP下载
|
||||
batchDownloadFiles: async (orderNo: string, onProgress?: (progress: number) => void) => {
|
||||
try {
|
||||
const res = await request('/api-order/files/download/batch', {
|
||||
method: 'POST',
|
||||
data: { orderNo },
|
||||
responseType: 'blob',
|
||||
// 获取当前token,用于请求头
|
||||
const token = typeof sessionStorage !== 'undefined' ? sessionStorage.getItem('access_token') : null;
|
||||
|
||||
return await new Promise<Blob>((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/api-order/files/download/batch', true);
|
||||
xhr.responseType = 'blob';
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
// 添加Authorization请求头(重要!)
|
||||
if (token) {
|
||||
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
// 监听下载进度
|
||||
// 注意:即使 Content-Length 未知,progress 事件仍会触发,可以获取 event.loaded
|
||||
xhr.addEventListener('progress', (event) => {
|
||||
if (onProgress) {
|
||||
if (event.lengthComputable) {
|
||||
// 如果知道总大小,计算百分比
|
||||
const percentComplete = Math.round((event.loaded / event.total) * 100);
|
||||
onProgress(percentComplete);
|
||||
console.log(`📊 批量下载进度: ${percentComplete}%`);
|
||||
} else {
|
||||
// 如果不知道总大小,直接传递已下载字节数
|
||||
onProgress(event.loaded);
|
||||
console.log(`📊 批量下载进度: ${event.loaded} bytes`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
xhr.addEventListener('load', () => {
|
||||
if (xhr.status === 200) {
|
||||
console.log('✅ 批量下载成功');
|
||||
resolve(xhr.response as Blob);
|
||||
} else {
|
||||
reject(new Error(`HTTP ${xhr.status}`));
|
||||
}
|
||||
});
|
||||
|
||||
xhr.addEventListener('error', () => {
|
||||
console.error('❌ 网络错误');
|
||||
reject(new Error('网络错误'));
|
||||
});
|
||||
|
||||
xhr.addEventListener('abort', () => {
|
||||
console.error('❌ 下载被中止');
|
||||
reject(new Error('下载被中止'));
|
||||
});
|
||||
|
||||
// 发送请求(请求头需要在open和send之间添加)
|
||||
xhr.send(JSON.stringify({ orderNo }));
|
||||
});
|
||||
return res;
|
||||
} catch (error) {
|
||||
console.error('❌ batchDownloadFiles error:', error);
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user