文件上传和下载

上传文件

下载文件

一、有文件的静态链接地址

二、通过接口返回文件数据下载
1、以blob类型返回

axios({
	method: "get",
	url:'https://xxx/download',
	responseType: "blob",
	headers: {
	"Content-Type": "application/json",
	token:'test-token'
	},
})
.then((res) => {
	console.log(res);
	if (200 == res.status && res.data && res.data instanceof Blob) {
		let docType = res.headers["content-type"];
		let blob = new Blob([res.data], { type: docType });
		//在本地的时候'content-disposition',但是生产或其他环境就是'Content-Disposition'
		let contentDis =
		res.headers["Content-Disposition"] ||
		res.headers["content-disposition"];
		let fileName = contentDis.split("filename=")[1];
		const elink = document.createElement("a");
		elink.download = window.decodeURI(fileName);
		elink.style.display = "none";
		elink.href = URL.createObjectURL(blob);
		document.body.appendChild(elink);
		elink.click();
		URL.revokeObjectURL(elink.href); // 释放URL 对象
		document.body.removeChild(elink);
	} else {
		console.error("downloadFile errors!");
	}
})
.catch(() => {
	console.error("downloadFile errors!");
});

其他方式参考:js实现文件下载有很多种方式,本文主要介绍7种。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值