前端下载多个文件链接整合为压缩包

前端下载多个文件链接整合为压缩包

javascript 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="downloadButton">下载</button>
</body>
<script src="https://cdn.jsdelivr.net/npm/jszip@3/dist/jszip.min.js"></script>
<script>
    const links = [
        'http://39.100.116.85:6001/File/CEcertificateFile/81899821-959d-40d1-ae30-3564a97eedcb_WHOLECE.pdf',
        'http://39.100.116.85:6001/File/CEcertificateFile/317b28f1-ceac-434a-b5b7-a5f3b2b258f5_WHOLECE.pdf'
    ];

    function downloadFile(url) {
        return new Promise((resolve, reject) => {
            fetch(url)
                .then(response => response.blob())
                .then(blob => resolve(blob))
                .catch(error => reject(error));
        });
    }

    document.getElementById('downloadButton').addEventListener('click', async function () {
        const zip = new JSZip();

        try {
            const promises = links.map(async link => {
                const fileName = link.substring(link.lastIndexOf('/') + 1);
                const fileBlob = await downloadFile(link);
                zip.file(fileName, fileBlob);
            });

            await Promise.all(promises);

            zip.generateAsync({ type: 'blob' }).then(content => {
                const zipFile = URL.createObjectURL(content);
                const a = document.createElement('a');
                a.href = zipFile;
                a.download = 'files.zip';
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            });
        } catch (error) {
            console.error('Download failed:', error);
        }
    });
</script>

</html>
相关推荐
BigTopOne7 分钟前
ArLiveLite 学习计划
前端
慧都小项25 分钟前
夜间Web回归跑到一半停了?TestComplete 15.83提升批量测试稳定性
前端·测试工具·数据挖掘·回归·汽车·web
两只羊ovo28 分钟前
Agent ≠ 聊天机器人:从一问一答到 ReAct 循环,到底差在哪?
前端
摇曳的精灵37 分钟前
前端缓存优化
前端·缓存·缓存优化
老王以为40 分钟前
走进 AI Agent 第二篇:决定 AI Agent 能力上限的关键技术
前端·人工智能·机器学习
何以解忧,唯有..1 小时前
Python 异常处理完全指南:从基础到高级实践
开发语言·前端·python
Buke..1 小时前
【小程序逆向】某游快爆 AI 逆向 sign参数:AI 辅助分析与 MCP 工具链实战
前端·人工智能·爬虫·python·小程序·notepad++
ly76892 小时前
CSS 从入门到进阶:系统掌握现代网页样式与布局
前端·css
SWAGGY..3 小时前
【C++初阶】:(15)模板进阶--从非类型参数到模板特化与分离编译
java·服务器·前端
YHHLAI3 小时前
从「跑分」到「干活」:Benchmark 与 GPT 5.6 的两种叙事
大数据·前端·人工智能·react.js·前端框架