导出excel
小于 1 分钟react
源码
要求后端响应来用于下载的 excel 文件
instance({
url,data,method: 'post', responseType: 'blob'
}).then( res => {
const blob = new Blob([res.data],{
type: res.data.type
})
const name = (res.headers['file-name'] as string) || fileName;// 默认取响应头中属性 file-name 作为文件名
const link = document.createElement('a')// 创建下载用 a 标签
link.download = decodeURIComponent(name)
link.href = URL.createObjectURL(blob)
document.body.append(link)
link.click()// 模拟点击下载
document.body.removeChild(link)// 移除节点
window.URL.revokeObjectURL(link.href)// 移除下载文件占用的内存
})
