vue中下载Excel模板文件

let fileName = '****';
downExcel(fileName).then(res => { // 请求下载接口
	// 处理返回的文件流
	const content = res;
	const blob = new Blob([content]);
	const fileName = "Excel文件名称" + ".xlsx";
	if ("download" in document.createElement("a")) {
		// 非IE下载
		const elink = document.createElement("a");
		elink.download = 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 {
		// IE10+下载
		navigator.msSaveBlob(blob, fileName);
	}
});

// 走后台接口 获取文件流
export function downExcel(fileName) { 
	return request({
		url:'**********?fileName=' + fileName,
                method:'get',
                headers: {
                    "Content-Type": "application/json;application/octet-stream"
                },
                responseType: "blob",
                params:''
	})
}

  

  

posted @ 2020-12-28 14:04  海里的鱼-L  阅读(970)  评论(0编辑  收藏  举报