文件流下载文件,zip/其他格式文件
const loading = this.$loading({
lock: true,
text: '下载中...',
spinner: 'el-icon-loading'
})
batchDownload(params).then((res) => {
//res格式:{data:二进制文件流
fileName:'xxxxx.zip'
}
loading.close()
const firstIndex = res.filename.indexOf('.')
const lastIndex = res.filename.lastIndexOf('.')
let name = ''
let suffix = ''
if (lastIndex !== -1) {
name = res.filename.slice(0, firstIndex)
suffix = res.filename.slice(lastIndex + 1)
}
///////////////如果返回的是 res.data 是 二进制文件流
const url = window.URL.createObjectURL(new Blob([res]))
/////////////////////如果返回的是 res.data 是 blob 格式,
const url = window.URL.createObjectURL(res.data)
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', decodeURI(name + '.' + suffix))
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
//文件下载接口要有一个属性 responseType: 'blob'
export function download(url, params) {
return request({
url: url + '?' + qs.stringify(params, { indices: false }),
method: 'get',
responseType: 'blob'
})
}
const loading = this.$loading({
lock: true,
text: '下载中...',
spinner: 'el-icon-loading'
})
download('/api/ctrmminio/download/' + row.id, {}).then(res => {
loading.close()
const firstIndex = res.filename.indexOf('.')
const lastIndex = res.filename.lastIndexOf('.')
let name = ''
let suffix = ''
if (lastIndex !== -1) {
name = res.filename.slice(0, firstIndex)
suffix = res.filename.slice(lastIndex + 1)
}
downloadFile(res, name, suffix)
}).catch((res) => {
this.$message({ message: '下载失败', type: 'warning' })
})