vue 或js 中解决 window.location.href下个多个文件只下载一个的问题, window.location.href不能同时下载多个文件问题解决

 //下载试卷
      downloadPapers() {
        //接口参数
        let obj = {
          id: this.$route.query.code,
          title: this.papersTitle, //试卷名称
        }
        //时间戳值
        this.timestamp = this.getCurrentTimestamp()
        //请求下载的接口
        exportExam(obj).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: '下载成功!',
              type: 'success',
            })
            console.log('下载', res)
            res.data.forEach((file, index) => {
              this.downloadFile(
                file,
                index == 0 ? this.timestamp + '试卷' : this.timestamp + '答案'
              )
            })
          }
        })
      },
      //阻止href多文件下载只下载一个问题方法
      downloadFile(url, filename) {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', url, true)
        xhr.responseType = 'blob'
        xhr.onload = () => {
          if (xhr.status === 200) {
            const blob = xhr.response
            const link = document.createElement('a')
            link.href = window.URL.createObjectURL(blob)
            link.download = filename
            link.click()
          }
        }
        xhr.send()
      },
      //生成时间戳的函数
      getCurrentTimestamp() {
        const date = new Date()
        date.setSeconds(0)
        date.setMilliseconds(0)
        return Math.floor(date.getTime() / 1000 / 60)
      },
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐