vuejs实现文件下载的三种方式
第一种方式是前端创建超链接,通过a标签的链接向后端服务发get请求,接收后端的文件流,非常简单:
1 <a :href='"/路径"' >下载模板</a>
另一种情况是创建div标签,动态创建a标签:
1 <div name="downloadfile" onclick="downloadExcel()">下载</div> 2 function downloadExcel() { 3 let a = document.createElement('a') 4 a.href ="路径" 5 a.click(); 6 }
还有一种补充:
1 function downloadExcel() { 2 window.location.href = "/路径"; 3 }
第二种方式通过创建iframe的方式:
1 <el-button size="mini" class="filter-item" type="primary" icon="el-icon-download" @click="handleExport(scope.row)">导出</el-button> 2 //method方法: 3 handleExport(row) { 4 var elemIF = document.createElement('iframe') 5 elemIF.src = '路径?snapshotTime=' + formatDate(new Date(row.snapshotTime), 'yyyy-MM-dd hh:mm') + 6 '&category=' + row.category 7 elemIF.style.display = 'none' 8 document.body.appendChild(elemIF) 9 }
第三种方式,会对后端发的post请求,使用blob格式:
1 <el-button size="mini" class="filter-item" type="primary" icon="el-icon-download" @click="handleExport(scope.row)">导出</el-button> 2 //method方法 3 handleExport(row){ 4 const url="/user/downloadExcel" 5 const options = {snapshotTime:formatDate(new Date(row.snapshotTime), 'yyyy-MM-dd hh:mm')} 6 exportExcel(url,options) 7 } 8 /** 9 * 封装导出Excal文件请求 10 * @param url 11 * @param data 12 * @returns {Promise} 13 */ 14 //api.js 15 export function exportExcel(url, options = {}) { 16 return new Promise((resolve, reject) => { 17 console.log(`${url} 请求数据,参数=>`, JSON.stringify(options)) 18 axios.defaults.headers['content-type'] = 'application/json;charset=UTF-8' 19 axios({ 20 method: 'post', 21 url: url, // 请求地址 22 data: options, // 参数 23 responseType: 'blob' // 表明返回服务器返回的数据类型 24 }).then( 25 response => { 26 resolve(response.data) 27 let blob = new Blob([response.data], { 28 type: 'application/vnd.ms-excel' 29 }) 30 console.log(blob) 31 let fileName = Date.parse(new Date()) + '.xlsx' 32 if (window.navigator.msSaveOrOpenBlob) { 33 // console.log(2) 34 navigator.msSaveBlob(blob, fileName) 35 } else { 36 // console.log(3) 37 var link = document.createElement('a') 38 link.href = window.URL.createObjectURL(blob) 39 link.download = fileName 40 link.click() 41 //释放内存 42 window.URL.revokeObjectURL(link.href) 43 } 44 }, 45 err => { 46 reject(err) 47 } 48 ) 49 }) 50 }
附:vue实现图片或文件下载功能实例
1,这里是调用接口之后如果code=200时进行下载:
1 if (res.code == 200) { 2 const link = document.createElement("a"); //自己创建的a标签 3 link.href = res.data; 4 document.body.appendChild(link); 5 link.click(); 6 document.body.removeChild(link); 7 window.URL.revokeObjectURL(res.data); 8 }
2、还有一种情况是下载单张图片,如果用上面方法会直接跳转到了图片链接,并不会实现下载。下面则是下载单张图片的方法:
1 getUrlBase64(imgUrl) { 2 return new Promise((resolve) => { 3 let canvas = document.createElement("canvas"); 4 let ctx = canvas.getContext("2d"); 5 let img = new Image(); //允许进行跨域 6 img.crossOrigin = "Anonymous"; 7 img.src = imgUrl; 8 img.onload = function() { 9 canvas.height = img.height; //图片的高度 10 canvas.width = img.width;//图片的宽度 11 ctx.drawImage(img, 0, 0, img.width, img.height); 12 let dataURL = canvas.toDataURL("image/png"); 13 canvas = null; 14 resolve(dataURL); 15 }; 16 }); 17 }, 18 //点击下载图片按钮的事件 19 handleDowondImg(url, name) { 20 this.getUrlBase64(url).then((base64) => { 21 const link = document.createElement("a"); 22 link.href = base64; 23 link.download = this.$route.query.source; 24 link.click(); 25 }); 26 },