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

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

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>
相关推荐
前端小趴菜055 分钟前
React - 组件通信
前端·react.js·前端框架
Amy_cx25 分钟前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing99939 分钟前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
后海 0_o1 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构
Scabbards_1 小时前
CPT304-2425-S2-Software Engineering II
前端
小满zs1 小时前
Zustand 第二章(状态处理)
前端·react.js
程序猿小D1 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
萌萌哒草头将军1 小时前
🚀🚀🚀Prisma 发布无 Rust 引擎预览版,安装和使用更轻量;支持任何 ORM 连接引擎;支持自动备份...
前端·javascript·vue.js
狼性书生2 小时前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
书语时2 小时前
ES6 Promise 状态机
前端·javascript·es6