AngularJS通过post方法下载excel文件
来源:互联网 发布:学美工一定要会摄影吗 编辑:程序博客网 时间:2024/06/06 17:32
注明:
第一行的:
$rootScope.restful_api.last_output_excel,body_data
这两个值,分别是URL串和post请求的body。其他代码不用变,即可使用。
原文链接:http://www.cnblogs.com/xujanus/p/5985644.html
最近工作中遇到,要使用angularJS的post方法来下载excel的情况。网上找到一个帖子:http://stackoverflow.com/questions/22447952/angularjs-http-post-convert-binary-to-excel-file-and-download ,改动了里面部分代码搞定。
详细代码:
$http.post($rootScope.restful_api.last_output_excel,body_data,{responseType: 'arraybuffer'}).success(function(data){ var blob = new Blob([data], {type: "application/vnd.ms-excel"}); var objectUrl = URL.createObjectURL(blob); var aForExcel = $("<a><span class='forExcel'>下载excel</span></a>").attr("href",objectUrl); $("body").append(aForExcel); $(".forExcel").click(); aForExcel.remove(); })
经验总结:
1.post的方法里要加responseType: 'arraybuffer'参数,不然下载的excel会乱码(这点一开始没注意到,费力好久)
2.使用{type: "application/vnd.ms-excel"}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx
3.使用增加节点调用click方法,而不使用帖子中的window.open(objectUrl)方法,是防止被浏览器当插件屏蔽弹出连接
阅读全文
0 0
- AngularJS通过post方法下载excel文件
- angular2/angular4 如何通过$http的post方法请求下载二进制的Excel文件
- angularjs 通过http下载php生成的excel的方法
- AngularJS如何下载Excel文件
- angular2 通过get方法请求下载二进制的Excel文件
- 使用AngularJS导出/下载excel文件
- javaWeb通过POST来下载文件
- angularjs实现POST方式下载文件,firefox兼容性问题
- AngularJS+Jersey下载excel
- excel文件下载实现方法
- php (带cookie)模拟get/post 方法下载excel
- 通过使用libcurl POST数据和上传与下载文件
- JAVA通过URL下载文件(POST,参数)
- 通过bower 下载管理angularjs
- Android通过SOCKET下载文件的方法
- 将文件通过http的post方法上传到服务器
- 在PHP中通过POST方法实现文件上传功能
- QT 通过POST方法,上传文件到服务器
- Android四大基本组件介绍与生命周期
- CMD文件、文件夹操作
- SpringBoot集成jdbcTemplate
- greendao数据的使用,简单形
- Java多线程中join方法的实现
- AngularJS通过post方法下载excel文件
- button 标记点击刷新页面的问题
- 去除inline-block元素间间距的N种方法-张鑫旭
- scala初学1之基础篇
- 针对某游戏保护DebugPort清零的一次逆向
- 扩展VLAN特性与管理——2
- C++中的友元类及友元类的应用场景
- UItableView 去掉多余的分割线
- Python类编码风格