一直复现不出来,
原因: 下载的太频繁, 并发了,一次下载十张图片
解决方法:
- await
- 加定时器,延迟一下
let _imgs = await Promise.all(
data.imgs.map((item) => {
return srcToFile(item.url);
}),
).catch((err) => {
message.error(err.message);
return [];
});
console.log(_imgs, "-----====----");
for (let index = 0; index < _imgs.length; index++) {
downloadByUrl(
URL.createObjectURL(_imgs[index]),
graphic?.title + `-${index + 1}`,
);
*//* 等待*500ms*在下载下一张,防止下载太快导致图片丢失
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
}
下载方法
export const downloadByUrl = (url: string, name?: string) => {
let id = "download" + name || "";
var a = document.createElement("a");
a.setAttribute("href", url);
a.setAttribute("id", id);
if (name) {
a.setAttribute("download", name);
}
a.setAttribute("target", "_self");
// 防止反复添加
if (!document.getElementById(id)) {
document.body.appendChild(a);
}
a.click();
};