vue 或js 中解决 window.location.href下个多个文件只下载一个的问题, window.location.href不能同时下载多个文件问题解决
【代码】vue 或js 中解决 window.location.href下个多个文件只下载一个的问题, window.location.href不能同时下载多个文件问题解决。
·
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)
},
更多推荐
已为社区贡献5条内容
所有评论(0)