Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jquery - Generate CSV file from javascript under IE11

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 += "
";
                msg.d.LstObj.forEach(function (rowArray) {
                    var row = '';
                    $.each(rowArray, function (key, element) { row += (row === '' ? '' : ',') + element; });
                    csvContent += row + "
";
                });
                var hiddenElement = document.createElement('a');
                hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvContent);
                hiddenElement.target = '_blank';
                hiddenElement.download = 'people.csv';
                hiddenElement.click();

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

voulez vous autoriser ce site web à ouvrir une application

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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=,
" + result);
    oWin.document.close();
    oWin.document.execCommand("SaveAs", true, csvFileName);
    oWin.close();
} else {
    //Everything Else
    window.open(result);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...