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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!