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

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

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>
相关推荐
GIS之路6 分钟前
GIS 数据转换:GDAL 实现将 CSV 转换为 Shp 数据(一)
前端
武清伯MVP17 分钟前
深入了解Canvas:HTML5时代的绘图利器(一)
前端·html5·canvas
一水鉴天29 分钟前
整体设计 定稿 之24 dashboard.html 增加三层次动态记录体系仪表盘 之2 程序 (Q208 之1)
前端·html
_杨瀚博36 分钟前
微信支付集成_JSAPI
前端
polaris_tl37 分钟前
react beginwork
前端
亮子AI1 小时前
【css】列表的标号怎么实现居中对齐?
前端·css
梦想的旅途21 小时前
媒体文件(图片/文件)的上传与管理:获取 Media ID 的技术细节
前端·http·servlet
一水鉴天1 小时前
整体设计 定稿 之22 dashboard.html 增加三层次动态记录体系仪表盘 之1
前端·html
张拭心2 小时前
程序员越想创业,越不要急着动手
前端·人工智能
舒一笑2 小时前
在低配云服务器上实现自动化部署:Drone CI + Gitee Webhook 的轻量级实践
前端·后端·程序员