Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading file custom name with jquery

Tags:

jquery

I am using a jQuery plugin to export HTML table to Excel. The downloaded file name using chrome is always download.xls and using mozilla firefox it is random-string.xls. I want file name to be created according to date. For example 23-06-2014.xls.

Below is the custom JS block in view file

$(document).ready(function () {
    $("#btnExport").click(function () {
        $("#account_table").btechco_excelexport({
            containerid: "account_table", 
            datatype: $datatype.Table
        });
    });
});
like image 333
S. M. Shahinul Islam Avatar asked Dec 06 '22 01:12

S. M. Shahinul Islam


2 Answers

You want to set the download and href attributes on the link. For example

$("#btnExport").click(function () {
        var uri = $("#account_table").btechco_excelexport({
            containerid: "account_table", 
            datatype: $datatype.Table,
            returnUri: true
        });

        $(this).attr('download', 'ExportToExcel.xls') // set file name (you want to put formatted date here)
               .attr('href', uri)                     // data to download
               .attr('target', '_blank')              // open in new window (optional)
        ;
    });
like image 105
Darren Kopp Avatar answered Jan 13 '23 01:01

Darren Kopp


If you do not want to use this plugin there is an alternative javascript already written by people. I just modified it to have the name of the file instead of the default one.

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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></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) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = {
                worksheet: name || 'Worksheet',
                table: table.innerHTML
            }
            var blob = new Blob([format(template, ctx)]);
            var blobURL = window.URL.createObjectURL(blob);
            return blobURL;
        }
})()

The jquery to set the name will be:

$("#btnExport").click(function () {
    var todaysDate = moment().format('DD-MM-YYYY');
    var blobURL = tableToExcel('account_table', 'test_table');
    $(this).attr('download',todaysDate+'.xls')
    $(this).attr('href',blobURL);
});

Example: Fiddle

References: Link1 Link2

like image 37
V31 Avatar answered Jan 13 '23 00:01

V31