Sonalkumar sute
Sonalkumar sute

Reputation: 2575

While exporting as csv file, file is getting downloaded but with name download and with no extension

While exporting as csv file, file is getting downloaded but with name as download and with no extension, I tried searching solution but didn't got any.

below is the code. If I rename the file and save as csv it shows the correct data

            _addPrintButton: function() {
                var me = this;
                this.down('#print_button_box').add( { 
                    xtype: 'rallybutton', 
                    itemId: 'print_button',
                    text: 'Export to Excel',
                    disabled: false,
                    margin: '20 10 10 0',
                    region: "right",                        
                    handler: function() {
                        me._onClickExport();
                    }
                });
            },  
            _onClickExport: function(){
                var grid = this.down('#grid_box');
                var data = this._getCSV(grid.items.items[0]);
                window.location = 'data:text/csv;charset=utf8,' + encodeURIComponent(data);
                //Ext.getBody().unmask();               
            },
            _getCSV: function (grid) {
                var cols    = grid.columns;
                var store   = grid.store;
                var data = '';
                var that = this;
                _.each(cols, function(col, index) {
                    data += that._getFieldTextAndEscape(col.text) + ',';
                });
                data += "\r\n";
                _.each(that.records, function(record) {
                    _.each(cols, function(col, index) {
                        var text = '';
                        var fieldName = col.dataIndex;
                        text = record[fieldName];
                        if (text || text == 0) {
                            //text = record[fieldName];
                            data += that._getFieldTextAndEscape(text) + ',';
                        }
                        /*else if (fieldName === "Project" ) {
                            text = record[fieldName];
                        }
                        else if (fieldName === "Case") {
                            var size = _.size(record[fieldName]);
                            for (var i = 0; i < size; i++){
                                text = record[fieldName][i]
                            }
                        }*/
                    });
                    data += "\r\n";
                });
                return data;
            },
            _getFieldTextAndEscape: function(fieldData) {
                var string  = this._getFieldText(fieldData); 
                return this._escapeForCSV(string);
            },
            _getFieldText: function(fieldData) {
                var text;
                if (fieldData === null || fieldData === undefined) {
                    text = '';
                } else if (fieldData._refObjectName) {
                    text = fieldData._refObjectName;
                }else {
                    text = fieldData;
                }
                return text.toString();
            },
            _escapeForCSV: function(string) {
                if (string.match(/,/)) {
                    if (!string.match(/"/)) {
                        string = '"' + string + '"';
                    } else {
                        string = string.replace(/,/g, ''); 
                    }
                }
                return string;
            },              

Upvotes: 0

Views: 864

Answers (1)

Peter Ivan
Peter Ivan

Reputation: 1521

You're searching at the wrong place.
The web server has to add HTTP header Content-Disposition to indicate the recommended file name, e.g.

Content-Disposition: attachment; filename="correct_data.csv"

Upvotes: 1

Related Questions