原生js下载word/pdf/xlsx/图片/

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<style>
			div {
				height: 50px;
			}
		</style>
	</head>
	<body>
		<div onclick="pdf()">下载pdf</div>
		<div onclick="word()">下载word</div>
		<div onclick="excel()">下载xlsx</div>
		<div onclick="jpg()">下载图片</div>
	</body>
	<script>
		function pdf() {
			let url = "http://test.yxycf.top/1.pdf"
			let list = {
				url,
				name: 'pdf文件',
				type: 'pdf'
			}
			downloadType(list)
		}
 
		function word() {
			let url = "http://test.yxycf.top/3.docx"
			let list = {
				url,
				name: 'word文件',
				type: 'word'
			}
			downloadType(list)
		}
 
		function excel() {
			let url = "http://test.yxycf.top/2.xlsx"
			let list = {
				url,
				name: 'excel文件',
				type: 'xlsx'
			}
			downloadType(list)
		}
 
		function jpg() {
			let url = "http://test.yxycf.top/4.jpg"
			let list = {
				url,
				name: '图片',
				type: 'img'
			}
			downloadType(list)
		}
 
		function downloadType(data) {
			switch (data.type) {
				case 'img':
					return downloadImg(data)
				case 'pdf':
					return downloadFile(data)
				case 'word':
					return downloadFile(data)
				case 'xlsx':
					return downloadFile(data)
			}
		}
 
		function downloadImg(data) {
			pathToBase64(data.url).then(res => {
				const link = document.createElement("a");
				link.href = res;
				link.setAttribute("download", data.name);
				document.body.appendChild(link);
				link.click();
				link.remove();
			}).catch(err => {
				console.log(err);
			})
		}
 
		function downloadFile(data) {
			fetchDownloadFile(data)
		}
 
		function fetchDownloadFile(data) {
			fetch(data.url, {
					method: "get",
					mode: "cors",
				})
				.then((response) => response.blob())
				.then((res) => {
					const downloadUrl = window.URL.createObjectURL(
						//new Blob() 对后端返回文件流类型处理
						new Blob([res], {
							type: data.type == "pdf" ? "application/pdf" : data.type == "word" ?
								"application/msword" : data.type == "xlsx" ? "application/vnd.ms-excel" : ""
						})
					);
					//word文档为msword,pdf文档为pdf
					const link = document.createElement("a");
					link.href = downloadUrl;
					link.setAttribute("download", data.name);
					document.body.appendChild(link);
					link.click();
					link.remove();
				}).catch((error) => {
					window.open(url);
				});
		};
 
		//获取Base64
		function pathToBase64(url) {
			return new Promise((resolve, reject) => {
				let image = new Image();
				image.onload = function() {
					let canvas = document.createElement('canvas');
					canvas.width = this.naturalWidth;
					canvas.height = this.naturalHeight;
					canvas.getContext('2d').drawImage(image, 0, 0);
					let result = canvas.toDataURL('image/png')
					resolve(result);
				};
				image.setAttribute("crossOrigin", 'Anonymous');
				image.src = url
				image.onerror = () => {
					reject(new Error('urlToBase64 error'));
				};
			})
		}
	</script>
</html>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玖逸少女梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值