async function downloadFile(url, filename) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const blob = await response.blob();
const downloadUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(downloadUrl);
document.body.removeChild(a);
} catch (err) {
console.error(err);
}
}
export async function batchDownload(fileUrls) {
const downloadPromises = fileUrls.map(async (url, index) => {
var filename = url.split("/").pop(); // 获取最后一个斜杠后的部分
var queryIndex = filename.indexOf("?"); // 查找问号的位置
if (queryIndex !== -1) {
filename = filename.substring(0, queryIndex); // 截取问号之前的部分作为文件名**文件名称根据实际情况命名,目前是截取部分url作为文件名**
}
return downloadFile(url, filename);
});
try {
await Promise.all(downloadPromises);
console.log('All files downloaded successfully');
} catch (err) {
console.error('Error occurred during batch download', err);
}
}
// 调用示例
const urls = [
'https://example.com/file1.pdf',
'https://example.com/file2.pdf',
// ... 其他文件URL
];
batchDownload(urls);
11-03
1111
05-28
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交