批量下载多个url文件

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);

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用matlab批量下载网站上的文件-MatlabUrlDownloadToFile.m 前几天美国数学建模成绩出了,我们按照网址下载了自己的证书,另外想看看学校今年的获奖情况,由于学校的参赛队伍数众多,一个一个下载证书再统计显得麻烦,于是想自己用matlab开发一个小程序以实现同类型文件批量下载。很多网站上有介绍使用Windows API 函数URLDownloadToFile,但是这个在VC上实现容易,要把它移植到matlab上还得使用MEX,有点麻烦了。这几天一直徘徊个大小网站,没有找到相关信息。最后还是自己使用lookfor找到需要的函UrlWrite,调用这个函数轻松搞定批量下载! 以下是代码: %使用matlab下载网站上的文件 %使用urlwrite函数下载网站上的文件 %特别适用于批量下载同类型的文件 % Created by longwen36 2011-4-16 %使用urlwrite实现批量下载 %以下载2011美赛数模证书为例 clc,clear; num = 11701:11710; URLs = cell); folder_filenames = cell); filenames = cell); for idx  = 1:length;     URLs{idx} = sprintf);     filenames{idx} = sprintf); end tic; for idx = 1:length;     fprintf;     [f, status] = urlwrite;     if status == 1;         fprintf;     else         fprintf;     end     end etime = toc; fprintf,etime); 复制代码 下载速度还挺快的,才发现,matlab功能确实很强大,函数很齐全。利用这个函数,加以改进,编写GUI,就可以做个简易的批量下载器了! 感兴趣的同仁可以试试!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值