vue.js前端实现excel表格导出和获取headers里的信息
前段时间写过一篇文章基于element实现后台管理系统,并提到excel表格导出功能,可能描述不是很详细,现在单独整理列出。
后端提供的接口:
// 下载分拣列表 export function getAssormentExport(params) { return request({ url: '/manage/health/assorment/export?ids=' + params, responseType: 'arraybuffer', // 代表内存之中的一段二进制数据 必须加 method: 'get' }) }
vue:点击按钮调用接口
<el-button type="primary" size="mini" @click="addexport()">导出选中</el-button>
script:
// 导出选中 addexport() { if (this.multipleSelection.length <= 0) { this.$message({ showClose: true, message: '未选中数据', type: 'error' }) return } for (let i = 0; i < this.multipleSelection.length; i++) { this.ids.push(this.multipleSelection[i].id) } // push一个新的数组,存储需要导出信息的ID并传给接口实现数据返回 getAssormentExport(this.ids).then( function(response) { const filename = decodeURI(response.headers['content-disposition'].split(';')[1].split('=')[1]) || '分拣表.xlsx' this.fileDownload(response.data, filename) // response.data是后端返回的二进制数据流,filename是获取到的导出文件名,获取失败使用默认值 this.ids = [] }.bind(this) ).catch( function(error) { this.$message({ showClose: true, message: error, type: 'error' }) this.ids = [] }.bind(this) ) }, fileDownload(data, fileName) { const blob = new Blob([data], { type: 'application/octet-stream' }) const filename = fileName || 'filename.xlsx' if (typeof window.navigator.msSaveBlob !== 'undefined') { window.navigator.msSaveBlob(blob, filename) } else { var blobURL = window.URL.createObjectURL(blob) var tempLink = document.createElement('a') tempLink.style.display = 'none' tempLink.href = blobURL tempLink.setAttribute('download', filename) if (typeof tempLink.download === 'undefined') { tempLink.setAttribute('target', '_blank') } document.body.appendChild(tempLink) tempLink.click() document.body.removeChild(tempLink) window.URL.revokeObjectURL(blobURL) } },
查看调用接口返回的信息
备注:
1.response返回了包含响应头所带的所有数据,可以使用console.log(response)查看打印数据,但是打印出来的数据只能拿到默认的响应头,这里有个需要注意的地方。
-
Cache-Control
-
Content-Language
-
Content-Type
-
Expires
-
Last-Modified
-
Pragma
如果想让浏览器能访问到其他响应头的话,需要后端在服务器上设置Access-Control-Expose-Headers
Access-Control-Expose-Headers : 'content-disposition'
后端大致写法:
headers.add("Access-Control-Expose-Headers", "Content-Disposition");
这样response.headers['content-disposition'].split(';')[1].split('=')[1] 就能取到接口返回的文件名称了。
2. decodeURI的使用
decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码。
实例
在本例中,我们将使用 decodeURI() 对一个编码后的 URI 进行解码:
<script type="text/javascript"> var test1="http://www.w3school.com.cn/My first/" document.write(encodeURI(test1)
+ "<br />") document.write(decodeURI(test1)
) </script>
输出:
http://www.w3school.com.cn/My%20first/ http://www.w3school.com.cn/My first/
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
分布式系列三: 对象序列化
序列化可以解决分布式系统节点间复杂对象传输的问题. 将对象状态转化为可存储或可传输的过程叫序列化, 而反序列化是将其还原成对象的过程. 几种序列化机制 JDK的序列化 Java默认的序列化要求实现Serializable接口. 缺点: 序列化的结果比较大, 占用字节多, 传输效率低 仅Java实现, 不能跨语言 WebService 基于XML格式的传输. Json方式 缺点: 结果依然较大 性能低 二进制传输 MessagePack Protocal Buffer Java的默认方式 // 使用了lombok @Data @AllArgsConstructor @NoArgsConstructor public class Car implements Serializable{ // 版本号, java通过此版本号是否一致判断是否可以执行反序列化 // 名称的数据类型必须准确,否则只是相当于定义一个普通的静态变量 public static long serialVersionUID = -1244L; // 静态变量不序列化 public static int age = 10;...
- 下一篇
11、python迭代器和生成器
迭代器和生成器 楔子 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,有几种方式? 首先,我可以通过索引取值l[0],其次我们是不是还可以用for循环来取值呀? 你有没有仔细思考过,用索引取值和for循环取值是有着微妙区别的。 如果用索引取值,你可以取到任意位置的值,前提是你要知道这个值在什么位置。 如果用for循环来取值,我们把每一个值都取到,不需要关心每一个值的位置,因为只能顺序的取值,并不能跳过任何一个直接去取其他位置的值。 但你有没有想过,我们为什么可以使用for循环来取值? for循环内部是怎么工作的呢? 迭代器 python中的for循环 要了解for循环是怎么回事儿,咱们还是要从代码的角度出发。 首先,我们对一个列表进行for循环。 for i in [1,2,3,4]: print(i) 上面这段代码肯定是没有问题的,但是我们换一种情况,来循环一个数字1234试试 for i in 1234 print(i) 结果:Traceback (most recent call last): File "test.py", line 4,...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS8安装Docker,最新的服务器搭配容器使用