m0_68177780 2023-02-21 11:53 采纳率: 54.5%
浏览 26
已结题

完成vue的批量下载的功能

这个是我下载文件的方法,我还需要一个批量下载的功能,是一个按钮的,哪位可告知一下
dowloadAgencyattach(data) {
      let fileIndex = data.fileRealName.lastIndexOf('/') + 1;
      let fileRealNewName = data.fileRealName.substring(fileIndex, data.fileRealName.length);
      axios.get(this.$vueAppBaseApi + '/resource/' + fileRealNewName,
        {
          responseType: 'blob'
        }).then(res => {
        if (res.status === 200) {
          let types = ''
          const index1 = data.fileRealName.lastIndexOf('.') + 1
          const index2 = data.fileRealName.length
          const file_type = data.fileRealName.substring(index1, index2)
          // 判断文件类型,补充type
          if (file_type === 'xlsx' || file_type === 'xls') {
            types = 'application/vnd.ms-excel'
          } else if (file_type === 'png') {
            types = 'application/x-png'
          } else if (file_type === 'jpg') {
            types = 'application/x-jpg'
          } else if (file_type === 'jpeg') {
            types = 'image/jpeg'
          }
          // res.data是后台返回的二进制数据,type:types为下载的数据类型
          const blob = new Blob([res.data], {
            type: types
          })
          const downLoadEle = document.createElement('a')
          const href = URL.createObjectURL(blob)
          downLoadEle.href = href
          // ooo为自定义文件名
          downLoadEle.download = fileRealNewName
          document.body.appendChild(downLoadEle)
          downLoadEle.click()
          document.body.removeChild(downLoadEle)
          window.URL.revokeObjectURL(href)
        }
      })
    }
  • 写回答

2条回答 默认 最新

  • MarkHan_ 2023-02-21 12:04
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ

    实现批量下载的功能可以通过循环遍历待下载的文件列表,并将下载代码放在循环体内执行。具体实现代码如下:

    batchDownloadAgencyattach(fileList) {
      for (let i = 0; i < fileList.length; i++) {
        const data = fileList[i]
        let fileIndex = data.fileRealName.lastIndexOf('/') + 1
        let fileRealNewName = data.fileRealName.substring(fileIndex, data.fileRealName.length)
        axios.get(this.$vueAppBaseApi + '/resource/' + fileRealNewName, {
          responseType: 'blob'
        }).then(res => {
          if (res.status === 200) {
            let types = ''
            const index1 = data.fileRealName.lastIndexOf('.') + 1
            const index2 = data.fileRealName.length
            const file_type = data.fileRealName.substring(index1, index2)
            // 判断文件类型,补充type
            if (file_type === 'xlsx' || file_type === 'xls') {
              types = 'application/vnd.ms-excel'
            } else if (file_type === 'png') {
              types = 'application/x-png'
            } else if (file_type === 'jpg') {
              types = 'application/x-jpg'
            } else if (file_type === 'jpeg') {
              types = 'image/jpeg'
            }
            // res.data是后台返回的二进制数据,type:types为下载的数据类型
            const blob = new Blob([res.data], {
              type: types
            })
            const downLoadEle = document.createElement('a')
            const href = URL.createObjectURL(blob)
            downLoadEle.href = href
            // ooo为自定义文件名
            downLoadEle.download = fileRealNewName
            document.body.appendChild(downLoadEle)
            downLoadEle.click()
            document.body.removeChild(downLoadEle)
            window.URL.revokeObjectURL(href)
          }
        })
      }
    }
    

    你需要将待下载的文件列表传入该方法中。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月3日
  • 已采纳回答 2月23日
  • 创建了问题 2月21日

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题