添加链接描述
点击如下实现图片下载
//下载
handleDownload(item) {
console.log("下载")
if (item.type == 1) {
this.$message({
message: '视频暂不支持下载',
type: 'warning'
})
return
}
let filename = new Date().getTime()
this.getBlob(item.url, (blob) => {
this.saveAs(blob, filename)
})
},
getBlob(url, cb) {
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
xhr.onload = function() {
console.log(xhr.response,'xhr.response====')
if (xhr.status === 200) {
cb(xhr.response)
}
}
xhr.send()
},
saveAs(blob, filename) {
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, filename)
} else {
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', filename)
if (typeof link.download === 'undefined') {
link.setAttribute('target', '_blank')
}
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
},
参考连接
https://www.cnblogs.com/anniey/p/15794190.html