Your Creation详情页
This commit is contained in:
@@ -45,4 +45,51 @@ const getMousePosition = (e:any,bor:any) => {
|
||||
export {
|
||||
getUniversalZoomLevel,
|
||||
getMousePosition,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 下载图片
|
||||
* @param list 图片列表
|
||||
* @param onProgress 下载进度回调
|
||||
* @param onError 下载错误回调
|
||||
* @param onSuccess 下载成功回调
|
||||
*/
|
||||
export async function DownloadImages(list:Array<{url:string,name:string}>, onProgress?:(count:number,total:number,item:any)=>void, onError?:(count:number,total:number,item:any)=>void, onSuccess?:(successCount:number,errCount:number)=>void) {
|
||||
const total = list.length;
|
||||
var count = 0;
|
||||
var successCount = 0;
|
||||
var errCount = 0;
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
await new Promise((resolve) => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", list[i].url);
|
||||
xhr.responseType = "blob"
|
||||
xhr.onload = function () {
|
||||
count++;
|
||||
if (this.status === 200) {
|
||||
let blob = this.response;
|
||||
const a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = list[i].name || list[i].url;
|
||||
a.click();
|
||||
successCount++;
|
||||
typeof onProgress === "function" && onProgress(count,total,list[i]);
|
||||
resolve(blob);
|
||||
} else {
|
||||
errCount++;
|
||||
typeof onError === "function" && onError(count,total,list[i]);
|
||||
resolve(true);
|
||||
}
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
count++;
|
||||
errCount++;
|
||||
typeof onError === "function" && onError(count,total,list[i]);
|
||||
resolve(true);
|
||||
};
|
||||
xhr.send();
|
||||
})
|
||||
}
|
||||
typeof onSuccess === "function" && onSuccess(successCount,errCount);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user