一、接口准备
// 下载word模板
getAttorneyTemplate(data){
return axiosApi({
method:'GET',
url:apiList.attorneyTemplate, //url接口
responseType:'blob', //字节流
data //参数
})
}
二、请求接口
//下载模板
getAttorneyTemplate(){
registrationApi.getAttorneyTemplate({fileKey:'...'}) //参数,根据后端需要
.then((response) => {
const res = response;
let url = window.URL.createObjectURL(res.data);
let link = document.createElement('a'); //创建a标签
link.style.display = 'none'; //样式无
link.href = url; //设置请求的url
// 指定文件名&文件类型(后缀名)
let fileName = `委托书模板.docx`;
/**
* 添加属性,并赋指定的值 el.setAttribute('download','zzz')
* demo: <a href="abc.gif" download="zzz">
* download属性的值即使当前要导出的文件的文件名
* */
link.setAttribute('download', fileName);
link.click(); //操作执行该a标签
// 释放创建的对象(创建的新的URL必须通过该方法释放)
window.URL.revokeObjectURL(url);
Message.success('下载成功!');
})
};
根据以上这个请求操作就可以实现下载到本地了,而且是你想要的格式word