项目中使用axiso下载 word文件

  • 通过axios的 post 向后台发起请求word文件流
function downWord(){
axios.post('/testDownExcelUrl',data,{
                responseType:'blob',
                header:{
                    'Content-Type':'application/json;charset=UTF-8',
                }
            }).then(res => {
                exportWord(res,'word文件');
            })
}
function exportWord(res,name){
    const blob = new Blob([res.data]);
    const fileName = name + '.word';
    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);
}
  • 如果设置了拦截器需要判断responseType == “blob” ,防止报错
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐