Alvi_1987 Alvi_1987 - 7 months ago 173
Javascript Question

Downloading file custom name with 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
});
});
});

Answer

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)
        ;
    });
Comments