Vue 下载 blob 流文件
下载 Excel
showFile(item) {
this.$http({
method: 'post',
url: '/fileApi/downLoadFile',
responseType: 'blob',
data: {
'flieName': item.fileName
},
}).then(data => {
if (!data) {
return
}
let link = document.createElement('a')
link.href = window.URL.createObjectURL(new Blob([data.data], {
type: "application/x-xls"
}))
link.target = '_blank'
link.download = decodeURI('123.xlsx')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
},
下载 txt
showFile(item) {
this.$http({
method: 'post',
url: '/fileApi/downLoadFile',
responseType: 'blob',
data: {
'flieName': item.fileName
},
}).then(data => {
if (!data) {
return
}
let link = document.createElement('a')
link.href = window.URL.createObjectURL(new Blob([data.data],))
link.target = '_blank'
link.download = decodeURI('文件名.txt')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
},
下载 zip
showFile(item) {
this.$http({
method: 'post',
url: '/fileApi/downLoadFile',
responseType: 'blob',
data: {
'flieName': item.fileName
},
}).then(data => {
if (!data) {
return
}
let link = document.createElement('a')
link.href = window.URL.createObjectURL(new Blob([data.data], {
type: "application/zip"
}))
link.target = '_blank'
link.download = decodeURI('123.zip')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
},
下载图片
this.$http({
url: '请求的接口',
method: 'get',
responseType: 'blob',
params: {
id: '123456',
index: 'img'
}
}).then(({ data }) => {
let blob = new Blob([data]);
let url = window.URL.createObjectURL(blob);
this.mapSrc = url
}
})