vue中本地文件下载

1.先把文件放在静态资源 public 中
vue中本地文件下载_第1张图片
2.给标签添加点击事件

下载模板

3.页面中引入axios

import axios from 'axios';

4.为了避免中文无法导出,将待导出文件名称改为英文 “ peoplecode.xls ” ,导出后的名称设置为中文名称 “ 员工工号.xls ”;

download () {          
    axios.get('file/peoplecode.xls', {   //静态资源文件夹public            
        responseType: 'blob',          
    }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]));            
        const link = document.createElement('a');            
        let fname = '员工工号.xls';            
        link.href = url;            
        link.setAttribute('download', fname);            
        document.body.appendChild(link);            
        link.click();          
    }).catch(error => {            
        console.log('error:'+JSON.stringify(error))          
    });        
},

你可能感兴趣的:(前端vue.js文件下载)