Anchor tag download attribute not working :Bug in Chrome 35.0.1916.114

后端 未结 6 1174
一整个雨季
一整个雨季 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=''+document.getElementById(elId).innerHTML+'
    '; 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

提交回复
热议问题