vue 文件下载与图片下载

文件下载

window.open(url,'_self')

图片下载

点击查看代码
handleDownload(imgSrc, name){
	const image = new Image();
	// 解决跨域 Canvas 污染问题
	image.setAttribute("crossOrigin", "anonymous");
	image.src = imgSrc;
	image.onload = () => {
		const canvas = document.createElement("canvas");
		canvas.width = image.width;
		canvas.height = image.height;
		const context = canvas.getContext("2d");
		context.drawImage(image, 0, 0, image.width, image.height);
		canvas.toBlob((blob) => {
			const url = URL.createObjectURL(blob);
			const a = document.createElement("a");
			a.download = name || "photo";
			a.href = url;
			a.click();
			a.remove();
			URL.revokeObjectURL(url);
		});
	};
},
posted @ 2023-01-12 11:37  你有我备注吗  阅读(126)  评论(0编辑  收藏  举报