async getProgress(url, fileName, that, num = 0, chunkSize = 1024 * 1024 * 3) {
try {
const headResponse = await fetch(url, { method: 'HEAD' });
const totalSize = parseInt(headResponse.headers.get('content-length'));
let receivedBytes = 0;
const chunks = [];
while (receivedBytes < totalSize) {
const end = Math.min(receivedBytes + chunkSize - 1, totalSize - 1);
const chunkResponse = await fetch(url, {
headers: { Range: `bytes=${receivedBytes}-${end}` }
});
if (!chunkResponse.ok) throw new Error('Chunk download failed');
const chunk = await chunkResponse.blob();
chunks.push(chunk);
receivedBytes += chunk.size;
const percent = Math.min(99, (receivedBytes / totalSize) * 100);
if (num === 0) {
that.percentage = percent;
} else if (percent > num) {
that.percentage = percent;
}
}
const fullBlob = new Blob(chunks);
const downloadUrl = window.URL.createObjectURL(fullBlob);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = fileName.includes('.') ? fileName : `${fileName}_${new Date().getTime()}.xlsx`;
document.body.appendChild(a);
a.click();
setTimeout(() => {
window.URL.revokeObjectURL(downloadUrl);
document.body.removeChild(a);
that.percentage = 100;
that.progressShow = false;
}, 1000);
const index = url.indexOf('uploads');
if (index !== -1) {
const str = url.slice(index);
that.$http.get('del_export_excel_files/?file=' + str);
}
} catch (error) {
console.error('Download failed:', error);
that.$message.error('下载失败');
that.progressShow = false;
}
},