提交 cfc0faf3 authored 作者: 钟是志's avatar 钟是志

4023 医保--参保通知批量下载功能优化

上级 3f5508dd
import React from 'react';
import { Modal, message, Progress } from 'antd';
export async function showDownloadProgressFromFileName(res, {ext, fileName}) {
const reader = res.body.getReader();
let contentLength = res.headers.get('Content-Disposition').split('filename=')[1]; // 文件大小从filename里面取
let nowTime = new Date().getTime();
if (contentLength && contentLength.includes('.')) {
contentLength = Number(contentLength.split('.')[0]); // 文件大小
if (contentLength > 0) {
message.info('正在获取文件,请耐心等待');
let receivedLength = 0;
let chunks = [];
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
chunks.push(value);
receivedLength += value.length;
// 下载进度
const progress = (receivedLength / contentLength) * 100;
if(new Date().getTime() - 1200 > nowTime) {
message.info('正在获取文件,请耐心等待,下载进度为' + progress.toFixed(2) + '%', 1);
nowTime = new Date().getTime();
}
// 将获取的百分比callback返回出去
// console.log(progress);
// console.log(`Reveived ${receivedLength} of ${contentLength}`, progress, 'progress');
}
let a = document.createElement('a');
a.style.display = 'none';
document.body.append(a);
const urlFile = window.URL.createObjectURL(new Blob(chunks, { type: ext }));
a.href = urlFile;
let filename = fileName;
if (ext && filename.indexOf('.') === -1) {
filename = filename + '.' + ext;
}
a.download = filename;
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(urlFile);
a = null;
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论