檔案流下載檔案,zip/其他格式檔案

泽泽生龙發表於2024-07-16
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)
})

相關文章