0

I am building a small list making script. I found this code on Stack Overflow and it works perfectly, but I want to have it put the file into a specific folder, not the downloads.

var tableToExcel = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
    , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
    return function (table, name, filename) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
        var d = new Date();
        var hh = d.getHours();
        var mm = d.getMinutes();
        var ss = d.getSeconds();
        var time = hh + ":" + mm + "." + ss; 
        document.getElementById("dlink").href = uri + base64(format(template, ctx));
        document.getElementById("dlink").download = time + filename;
        document.getElementById("dlink").click();

    }
})()

The code is run by a button like so

<input type="button" onclick="tableToExcel('tablename', 'name', '.xls')" value="Export to Excel" >

And this link is necessary to facilitating the download.

<a id="dlink" style="display:none;"></a>

NOTE The part in the middle about the new date() is for automatically naming the file.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
The FireY
  • 3
  • 3

1 Answers1

0

The folder for a file to be downloaded to is usually set by the browser, not by Javascript running on a web page. Review your browser's Help section for instruction on how to change the default file download location.

Firefox

IE

Chrome

Safari

Additionally, I just came across this post that discusses browser-specific methods of accessing the file system using Javascript

Community
  • 1
  • 1
lsnare
  • 114
  • 6
  • The link to the slideshow about html5 methods of storage is perfect. I am going to try to make localstorage work, i will keep the thread updated if i find a realy good method. – The FireY Dec 22 '16 at 14:01