Anchor tag download attribute not working :Bug in Chrome 35.0.1916.114

后端 未结 6 1159
一整个雨季
一整个雨季 2020-11-29 04:21

I am trying to refer to this code where a we are downloading a CSV file on click of a link.

$(document).ready(function () {

    function exportTableToCSV($         


        
相关标签:
6条回答
  • 2020-11-29 05:07

    Here is a bit that worked for me (in Chrome and Firefox). I'm building a xls file out of a table.

      function downloadInnerHtml(filename,elId,mimeType){
        var elHtml='<table border="1">'+document.getElementById(elId).innerHTML+'</table>';
        var link=document.createElement('a');
        mimeType=mimeType || 'application/xls';
    
        var blob=new Blob([elHtml],{type:mimeType});
        var url=URL.createObjectURL(blob);
        link.href=url;
    
        link.setAttribute('download', filename);
        link.innerHTML = "Export to CSV";
        document.body.appendChild(link);
        link.click();
      }
    
      $(document).on("click","#exportButton",function(){
        var date=new Date();
        var mm=date.getMonth()+1;
        var dd=date.getDate();
        var yy=date.getFullYear();
        var timeStamp=yy+""+((mm<10)?"0"+mm:mm)+""+((dd<10)?"0"+dd:dd);
        var fileName=timeStamp+'_Employees.xls';
        downloadInnerHtml(fileName,'mainEmployeeTable','application/xls');
      });
    

    Hope that helps someone else...

    --Charles

    0 讨论(0)
  • 2020-11-29 05:08

    For me worked:

    var content = "some content";
    var link = document.createElement('a');
    var blob = new Blob(["\ufeff", content]);
    var url = URL.createObjectURL(blob);
    link.href = url;
    link.setAttribute('download', 'file.csv');
    link.click();
    
    0 讨论(0)
  • 2020-11-29 05:10

    When I tried this code, I was not able to get results in IE plus this code only iterates over td, it will skip any th present in your table. I have modified the code to resolve both the issues I was facing:

    $(document).ready(function () {
        function exportTableToCSV($table, filename) {
    
                var $rows = $table.find('tr'),
                tmpColDelim = String.fromCharCode(11),
                tmpHeadDelim = String.fromCharCode(11),
                tmpRowDelim = String.fromCharCode(0),
                colDelim = '","',
                headDelim = '","',
                rowDelim = '"\r\n"',
    
                csv = '"' + $rows.map(function (i, row) {
                    var $row = $(row),
                    $cols = $row.find('td');
                    $heads = $row.find('th');
    
                    var c = $heads.map(function (k, head) {
                        var $head = $(head),
                        text = $head.text();
                        return text.replace(/"/g, '""');
                    }).get().join(tmpHeadDelim);
    
                    var d = $cols.map(function (j, col) {
                        var $col = $(col),
                        text = $col.text();
                        return text.replace(/"/g, '""');
                    }).get().join(tmpColDelim);
    
                return (c+d);
    
            }).get().join(tmpRowDelim)
                .split(tmpRowDelim).join(rowDelim)
                .split(tmpHeadDelim).join(headDelim)
                .split(tmpColDelim).join(colDelim) + '"';
    
    
            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");
    
            // if Internet Explorer (10+)
            if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
                var blob = new Blob([decodeURIComponent(csv)], {
                        type: 'text/csv;charset=utf8'
                });
                window.navigator.msSaveBlob(blob, filename);
            }
            else {
                var link = document.createElement('a');
                var blob = new Blob([csv],{type:'text/csv;charset=utf8'});
                var url = URL.createObjectURL(blob);
                link.href = url;
                link.setAttribute('download', filename);
                document.body.appendChild(link);
                link.click();
            }
        }
    
        $("#fnExcelReport").on('click', function (event) {
            var args = [$('#tableContent'), 'Report.csv'];
            exportTableToCSV.apply(this, args);
        });
    });
    

    CodePen

    0 讨论(0)
  • 2020-11-29 05:15

    So you should change this:

    // Data URI
    csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
    
    $(this)
        .attr({
        'download': filename,
            'href': csvData,
            'target': '_blank'
    });
    

    To This

    // Data URI
    //csvData = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv), //old way
    blob = new Blob([csv], { type: 'text/csv' }); //new way
    var csvUrl = URL.createObjectURL(blob);
    
    $(this)
    .attr({
        'download': filename,
        'href': csvUrl
    });
    

    And it should work!

    0 讨论(0)
  • 2020-11-29 05:15

    This is a bug in Chrome 35 reported in issue #377860.

    Update: This bug was merged into issue #373182. You can click the star to tell the devs you think this issue should get fixed and also get notified of changes.

    Typically the chromium team releases an update every 2 weeks so you can expect a fix soon since this is marked as Pri-1 which I assume means highest priority.

    0 讨论(0)
  • 2020-11-29 05:18

    append anchor tag using string add tag information in string and then append, it works for me for google chrome

    0 讨论(0)
提交回复
热议问题