Add extension for exported CSV file

I have a script that exports data from a page to csv and other formats. The problem is that when saved to csv, the script exports it as a file without an extension. In addition, I can not change the file name - download.

Here is the part of the function that is designed to export csv:

(function($){
    $.fn.extend({
        tableExport: function(options) {
            var defaults = {
                    separator: ',',
                    ignoreColumn: [],
                    tableName:'yourTableName',
                    type:'csv',
                    pdfFontSize:14,
                    pdfLeftMargin:20,
                    escape:'true',
                    htmlContent:'true',
                    consoleLog:'true'
             };

            var options = $.extend(defaults, options);
            var el = this;

            if(defaults.type == 'csv' || defaults.type == 'txt'){

                // Header
                var tdData ="";
                $(el).find('thead').find('tr').each(function() {
                tdData += "\n";                 
                    $(this).filter(':visible').find('th').each(function(index,data) {
                        if ($(this).css('display') != 'none'){
                            if(defaults.ignoreColumn.indexOf(index) == -1){
                                tdData += '"' + parseString($(this)) + '"' + defaults.separator;                                    
                            }
                        }

                    });
                    tdData = $.trim(tdData);
                    tdData = $.trim(tdData).substring(0, tdData.length -1);
                });

                // Row vs Column
                $(el).find('tbody').find('tr').each(function() {
                tdData += "\n";
                    $(this).filter(':visible').find('td').each(function(index,data) {
                        if ($(this).css('display') != 'none'){
                            if(defaults.ignoreColumn.indexOf(index) == -1){
                                tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
                            }
                        }
                    });
                    //tdData = $.trim(tdData);
                    tdData = $.trim(tdData).substring(0, tdData.length -1);
                });

                //output
                if(defaults.consoleLog == 'true'){
                    console.log(tdData);
                }
                var base64data = "base64," + $.base64.encode(tdData);
                window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
            }

How can I add a CSV to an exported file and end up renaming the exported file?

+4
source share

All Articles