js前端解决浏览器下载兼容性问题
2015-07-22 09:48
519 查看
问题描述:js文件中有一个excel二进制流data,是后台传来的,前端负责将它下载下来,保存为 .xlsx 文件。在chrome和火狐上都没问题,偏偏ie11就是不行。
解决途径:通过google搜索,查到github和stackoverflow上有相似问题,然后下载github的代码来看,然后解决。
判断浏览器类型:
非IE浏览器下载写法:
兼容IE下载:
相关链接:
https://github.com/SheetJS/js-xlsx
https://github.com/eligrey/FileSaver.js
解决途径:通过google搜索,查到github和stackoverflow上有相似问题,然后下载github的代码来看,然后解决。
判断浏览器类型:
var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0; if (!!window.ActiveXObject || "ActiveXObject" in window || Sys.ie) {//Js判断为IE浏览器
非IE浏览器下载写法:
var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=UTF-8"});//"text/plain;charset=utf-8" var objectUrl = window.URL.createObjectURL(blob); var a = document.getElementById('downList'); a.href = objectUrl; a.download = 'user_list'; a.click();
兼容IE下载:
var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=UTF-8"}); saveAs(blob, "report.xlsx"); 使用到了一个js文件:FileSaver.min.js
相关链接:
https://github.com/SheetJS/js-xlsx
https://github.com/eligrey/FileSaver.js
// var a = document.createElement("a"); // document.body.appendChild(a); // //a.style = "display: none"; // var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}); // var objectUrl = window.URL.createObjectURL(blob); // a.href = objectUrl; // a.download = 'templdate'; // a.click(); // setTimeout(function () { // document.body.removeChild(a); // window.URL.revokeObjectURL(objectUrl); // }, 10000);
相关文章推荐
- jsp的onclick事件跳转js时,被省去位数前面的0
- jQuery图表开源软件
- 使用JavaScript的Canvas模拟CSS3中的background-size:cover;
- Fedora22相关配置
- 解决 iOS webkit 使用CSS动画时闪烁的问题
- AngularJS 学习路线
- 详解js跨域问题
- HTML5的设计目的是为了在移动设备上支持多媒体
- Visual Studio Professional 2015 (x86 and x64) - DVD (Chinese-Simplified)
- 参数传递的四种形式----- URL,超链接,js,form表单
- 【阅读】《head first html5》第三章——事件与处理程序
- 用于实现瀑布流图的jQuery插件
- javascript 实现HashTable(哈希表)
- 【HTML】html5新属性-datalist
- 基于jQuery虾米音乐播放器样式代码
- CSS display
- PHP结合jQuery实现红蓝投票功能特效
- 有几数组表单,js怎么获得数组并动态相加输出到文本框
- 安卓中的JSON解析
- javascript中的this