记录vue3文件下载

const onDownload = val => {
	ElMessageBox.confirm("确定下载该数据?", "Warning", {
		confirmButtonText: "确定",
		cancelButtonText: "取消",
		type: "warning"
	}).then(async () => {
		const token = getToken();
		// url 后端下载地址
		axios({
			method: "get",
			url: dataBaseUrl + `/address/sy/sync/downloadFail?recordsId=${val.id}`,
			responseType: "blob",
			headers: { Authorization: token, Token: token }
		}).then(function (res) {
			// 根据后端写的下载接口 考虑是否要这段代码
			// let contentDisposition = decodeURI(res.headers["content-disposition"]);
			// let fileRegex = /([^(filename=)]+\.xlsx?)$/g;
			// let result = contentDisposition.match(fileRegex);
			// let fileName = "失败记录.xlsx";
			// if (result.length > 0) {
			// 	fileName = result[0];
			// }

			let blob = new Blob([res.data]);
			let url = window.URL.createObjectURL(blob); // 创建 url 并指向 blob
			let a = document.createElement("a");
			a.href = url;
			a.download = "失败记录.xlsx";
			a.click();
			window.URL.revokeObjectURL(url); // 释放该 url

			ElMessage({
				type: "success",
				message: "操作成功!"
			});
		});
	});
};

你可能感兴趣的:(前端,javascript,开发语言)