download:post下载
程序员文章站
2022-07-03 21:30:20
本文写了一个ts封装的小方法,download下载功能的实现,首先建立一个ts文件:import axios from 'axios'//post 下载export const download = (url: string, obj: any, fileName: string, format: string): void => { axios.request({ method:'post', url: url, data: obj, responseType:'arraybuffe...
本文写了一个ts封装的小方法,download下载功能的实现,
首先建立一个ts文件:
import axios from 'axios'
//post 下载
export const download = (url: string, obj: any, fileName: string, format: string): void => {
axios.request({ method:'post', url: url, data: obj, responseType:'arraybuffer' }).then((response: any) => {
let data: any = response.data
if (!data) {
return
}
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.download = fileName + format; //下载后文件名
link.setAttribute('download', fileName + format)
document.body.appendChild(link)
link.click()
})
}
download = (url: string, obj: any, fileName: string, format: string): void
参数: {
url: 下载地址
obj: 参数对象
fileName: 文件名称
format: 下载的文件格式
}
eg: download(‘https://www.baidu.com/’, { id: 14138 }, ‘这是个文件’, ‘.txt’)
本文地址:https://blog.csdn.net/m0_45043105/article/details/107559390