当前位置: 首页 > Web前端 > vue.js

vue中本地文件下载

时间:2023-03-31 17:47:52 vue.js

1.先把文件放到静态资源public2.在标签上添加点击事件下载模板3.在页面中引入axiosimportaxiosfrom'axios';4、为避免中文无法导出,将导出文件的名称改为英文“peoplecode.xls”,并将导出的名称设置为中文名称“employeeID.xls”;download(){axios.get('file/peoplecode.xls',{//静态资源文件夹publicresponseType:'blob',}).then(response=>{consturl=window.URL.createObjectURL(newBlob([response.data]));constlink=document.createElement('a');letfname='员工ID.xls';link.href=url;link.setAttribute('download',fname);document.body.appendChild(link);link.click();}).catch(error=>{console.log('error:'+JSON.stringify(error))});},