import axios from '@/utils/utils'
const request = axios()
const createAnchorLink = (href,filename = '') => {
const a = document.createElement('a');
a.download = filename
a.href = href
document.body.appendChild(a)
a.click()
a.remove()
}
const downloadAll = ({
url,
filename,
callback,
type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel",
method = "POST",
data
}) => {
request({
url,
method,
reponseType: "blob",
getResponse:true,
data
}).then(res => {
let blob = new Blob([res.data],{type})
const blobUrl = window.URL.createObjectURL(blob)
createAnchorLink(blobUrl,filename)
window.URL.revokeObjectURL(blobUrl)
callback & callback(true)
}).catch(err => {
callback & callback(false)
})
}
export {downloadAll}
封装文件下载
最新推荐文章于 2024-06-13 10:31:50 发布