导出为csv文件时,文件正在下载但名称下载且没有扩展名

时间:2015-02-10 13:27:28

标签: csv extjs rally export-to-csv

导出为csv文件时,文件已下载,但名称为下载且没有扩展名, 我尝试过搜索解决方案,但没有得到任何解决方案。

下面是代码。如果我重命名文件并保存为csv,则显示正确的数据

            _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;
            },              

1 个答案:

答案 0 :(得分:1)

你在错误的地方搜索 Web服务器必须添加HTTP标头Content-Disposition以指示推荐的文件名,例如

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