axios使用get请求下载资源
axios使用get请求下载资源问题下载类似这种非静态文件资源地址时, 需要将axios默认的响应类型进行修改, 在获取到数据后写入文件https://xxx.xxx/download解决let resUrl: string = "https://xxx.xxx/download"axios.create({timeout: 3000,responseType: "blob", // 响应类型,
·
axios使用get请求下载资源
问题
下载类似这种非静态文件资源地址时, 需要将axios默认的响应类型进行修改, 在获取到数据后写入文件
https://xxx.xxx/download
解决
let resUrl: string = "https://xxx.xxx/download"
axios.create({
timeout: 3000,
responseType: "blob", // 响应类型, 将响应数据转换为二进制数据
headers: {},
})
.get(resUrl)
.then((res: any) => {
console.log(res);
// 地址转换
let url = window.URL.createObjectURL(res.data);
// 文件名
let fileName: string = "fileName.xxx";
const a = document.createElement("a");
a.setAttribute("href", url);
a.setAttribute("download", fileName);
document.body.append(a);
a.click();
document.body.removeChild(a);
});
更多推荐
已为社区贡献1条内容
所有评论(0)