热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Vue前端导出、下载zip/excel文件流blob

import.mockindex.js;该项目所有请求使用mockjs模拟去掉mockresponseType:blob,letblobnewBlob([response.

// import './mock/index.js'; // 该项目所有请求使用mockjs模拟 去掉mock

responseType: "blob",   

let blob = new Blob([response.data], { type: "application/zip" }); //注意是response 或者 response.data
let url = window.URL.createObjectURL(blob);  这三句最重要!!!

//普通代码
axios.post(postUrl, params, {responseType: 'arraybuffer'}).then(res => {// 创建Blob对象,设置文件类型let blob = new Blob([res.data], {type: "application/vnd.ms-excel"})let objectUrl = URL.createObjectURL(blob) // 创建URLlocation.href = objectUrl;URL.revokeObjectURL(objectUrl); // 释放内存
})

downloadAll() {axios({method: "get",url: "api/TemplateDownload/GetAllTemplateZIP",headers: {"content-type": "application/json; charset=utf-8",Authorization: COOKIEs.get("token") || "",},responseType: "blob",}).then((response) => {let blob = new Blob([response.data], { type: "application/zip" });let url = window.URL.createObjectURL(blob);const link = document.createElement("a"); // 创建a标签link.href = url;link.download = "模板下载"; // 重命名文件link.click();URL.revokeObjectURL(url); // 释放内存this.checkList = [];}).catch((error) => {console.log(error.data);});},//excel
let blob = new Blob([response.data], { type: "application/vnd.ms-excel" });dl() {axios({method: "get",url: "http://10.180.170.3:8794/tRptMwStdClt/exportData?time=202104",responseType: "arraybuffer",}).then((response) => {console.log(response);let blob = new Blob([response.data], {type: "application/vnd.ms-excel",});let url = window.URL.createObjectURL(blob);const link = document.createElement("a"); // 创建a标签link.href = url;link.download = "模板下载"; // 重命名文件link.click();URL.revokeObjectURL(url); // 释放内存this.checkList = [];}).catch((error) => {console.log(error.data);});},

获取到了后台传过来的excel文件 前端用vue怎么接收并导出? - 中文 - Vue Forum

vue.js前端实现excel表格导出和获取headers里的信息 - 五个半柠檬 - OSCHINA - 中文开源技术交流社区

java后台需要设置

response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");response.setHeader("Access-Control-Expose-Headers","Content-Disposition");才能获取到文件名等信息

// 导出execel2handleExcel() {this.$http({url: this.$http.adornUrl(url),method: "post",responseType: "blob",//!!!!params: this.$http.adornParams({userAccount: this.userName,}),}).then((res) => {// console.log(res, "res");let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });let fileName = decodeURI(response.headers["content-disposition"].split(";")[1].split("=")[1]);let url = window.URL.createObjectURL(blob);const link = document.createElement("a"); // 创建a标签link.href = url;link.download = fileName; // 重命名文件link.click();URL.revokeObjectURL(url); // 释放内存});},


推荐阅读
author-avatar
lixinleslee
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有