在IE11下从JavaScript生成CSV文件 [英] Generate CSV file from javascript under IE11

查看:340
本文介绍了在IE11下从JavaScript生成CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多示例,以便从数据中生成csv文件,然后将其推送以下载以导出.

I read lot of sample to generate csv file from data and push it to download to export it.

 let csvContent = '';
                $.each(msg.d.LstObj[0], function (key, element) { csvContent += (csvContent === '' ? '' : ',') + key; });
                csvContent += "\n";
                msg.d.LstObj.forEach(function (rowArray) {
                    var row = '';
                    $.each(rowArray, function (key, element) { row += (row === '' ? '' : ',') + element; });
                    csvContent += row + "\n";
                });
                var hiddenElement = document.createElement('a');
                hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvContent);
                hiddenElement.target = '_blank';
                hiddenElement.download = 'people.csv';
                hiddenElement.click();

在Chrome FF下:好的 在IE11下:没有下载,只是一条消息问我:

Under Chrome FF : ok Under IE11 : no download just a message ask me :

voulez vous autoriser ce网站上的应用程序

voulez vous autoriser ce site web à ouvrir une application

只有一个选择窗口可以存储... 有人有一个主意吗???我将代码放在"site de confiance"中……

And just one choise windows store... Someone have an idea??? I put my code in "site de confiance"...

推荐答案

这是我用来满足所有浏览器的功能块,其中包括IE 11,对我来说非常有用:

Here is the block I use to satisfy all browsers, IE 11 included and it works great for me:

if (window.navigator.msSaveBlob) {
    //Internet Explorer
    window.navigator.msSaveBlob(new Blob([result]), csvFileName);
} else if (window.webkitURL != null) {
    //Google Chrome and Mozilla Firefox
    var a = document.createElement("a");
    result = encodeURIComponent(result);
    a.href = "data:application/csv;charset=UTF-8," + result;
    a.download = csvFileName;
    a.click();
} else if (navigator.appName === "Microsoft Internet Explorer") {
    //Internet Explorer 8 and 9
    var oWin = window.open();
    oWin.document.write("sep=,\r\n" + result);
    oWin.document.close();
    oWin.document.execCommand("SaveAs", true, csvFileName);
    oWin.close();
} else {
    //Everything Else
    window.open(result);
}

这篇关于在IE11下从JavaScript生成CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆