sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

  • 方法-Function

  • 字符串-String

  • 数学-Math

  • 文件-Buffer

  • 节点-dom

  • 拓展

    • 序言 👏
    • exportPdf【导出PDF】
    • lazyLoadImg【懒加载图片】
    • protocolCheck【协议检测】
    • batchDownloadZip 【批量下载/压缩包.zip】
      • 1.依赖 jszip 和 file-saver 库安装
      • 2.示例
      • 3.入参说明
        • 3.1 主参数说明
      • 4.源码
    • storage【持久化存储】 🔥
  • nodejs

  • 指南
  • 拓展
bianpengfei
2022-04-05
目录

batchDownloadZip 【批量下载/压缩包.zip】

描述

批量下载文件,下载形式为(压缩包.zip)

⚠️ 警告

使用该方法,是依赖 jszip (opens new window) 和 file-saver (opens new window) 这两个库。 在本 utils 工具库中默认没有安装,需要使用者在自己的项目中安装。
我们推荐版本 jszip@3.7.1 file-saver@2.0.2

# 1.依赖 jszip 和 file-saver 库安装


    yarn add jszip@3.7.1 file-saver@2.0.2
    
    1
    npm i jszip@3.7.1 file-saver@2.0.2
    
    1
    // Make sure to add code blocks to your code group

    # 2.示例

    下载zip
    <template>
      <div ref="tableWrap"></div>
      <div style="margin-top: 20px">
        <el-button @click="onDownloadZip">下载zip</el-button>
      </div>
    </template>
    
    <script>
    import batchDownloadZip from 'sf-utils2/lib/expand/batchDownloadZip.js'
    export default {
      data() {
        return {
          urls: [
            {
              url: 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.31/vue.cjs1111.js', // url
              name: 'hellowrold.js' // 文件名
            },
            'https://cdn.jsdelivr.net/npm/sf-utils2/lib/b-utils.min.js'
          ]
        }
      },
    
      methods: {
        async onDownloadZip() {
          const res = await batchDownloadZip(this.urls, '例子')
    
          console.log(res)
        }
      }
    }
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    显示代码 复制代码 复制代码

    # 3.入参说明

    # 3.1 主参数说明

    参数 说明 类型 是否必填 默认值
    urlList url 集合 Array<String>
    Array<Object>
    - -
    fileName 导出的文件名,不要加扩展名 String - -

    如果 urlList 集合里面的参数,既可以是String,又可以是Object。

    例子 1:

    batchDownloadZip(['http://compassedu.hk/1.img', 'http://compassedu.hk/2.img'], '例子 1')
    
    1

    例子 2:

    batchDownloadZip(
      [
        {
          url: 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.31/vue.cjs1111.js',
          name: 'hellowrold.js'
        },
        'https://cdn.jsdelivr.net/npm/sf-utils2/lib/b-utils.min.js'
      ],
      '例子 2'
    )
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    # 4.源码

    源码,点开查看 👈
    import JSZip from 'jszip/dist/jszip.min.js'
    import FileSaver from 'file-saver'
    import getHttpBlob from '../dom/getHttpBlob'
    import isPlainObject from '@/base/isPlainObject'
    import isString from '@/base/isString'
    import uuid from '@/base/uuid'
    
    /**
     *  批量下载 (压缩包.zip)
     * @param urlList   由 url 拼接成的数组 如:['http://compassedu.hk/1.img', 'http://compassedu.hk/2.img'] 或者
     * [
     *         {
     *           url: 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.31/vue.cjs1111.js',
     *           name: 'hellowrold.js'
     *         },
     *         'https://cdn.jsdelivr.net/npm/sf-utils2/lib/b-utils.min.js'
     * ],
     * @param fileName  压缩包名称
     * @returns {Promise<*[]>}
     */
    export async function batchDownloadZip(urlList = [], fileName) {
      const _urlList = urlList.filter(Boolean)
      const zip = new JSZip()
      const result = []
      const res = await Promise.allSettled(_urlList.map(v => getHttpBlob(v.url || v)))
      res.forEach((v, vi) => {
        let fileName = uuid() // 文件名
        let url = '' // 远程的url地址
        if (isPlainObject(_urlList[vi])) {
          fileName = _urlList[vi].name || _urlList[vi].url?.split('/')?.slice()?.pop()
          url = _urlList[vi].url
        } else if (isString(_urlList[vi])) {
          fileName = _urlList[vi]?.split('/')?.slice()?.pop() || fileName
          url = _urlList[vi]
        }
        if (v.status == 'fulfilled') {
          zip.file(fileName, v.value, {
            binary: true
          })
        }
        result.push({
          ...v,
          url,
          name: fileName,
          binary: v.value
        })
      })
      const blobContent = await zip.generateAsync({
        type: 'blob'
      })
      FileSaver.saveAs(blobContent, `${fileName}.zip`) // 利用file-saver保存文件
      return result
    }
    
    export default batchDownloadZip
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    上次更新: 2025/07/01, 14:52:29
    protocolCheck【协议检测】
    storage【持久化存储】 🔥

    ← protocolCheck【协议检测】 storage【持久化存储】 🔥→

    Theme by Vdoing | Copyright © 2022-2025 bianpengfei
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    ×