2015-02-10 58 views
0

在導出爲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

您正在搜索錯誤的地方。
網絡服務器必須添加HTTP報頭Content-Disposition以指示推薦的文件名,例如,

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

感謝您的答覆,但如果你要我把這個我試過像這樣'了window.location =「內容處置:附件;文件名=‘correct_data.csv’的charset = UTF8,」 + encodeURIComponent方法(數據);'但它不正確 – Sontya 2015-02-11 08:17:06

+1

它在答案中說。 「Web服務器必須添加HTTP標頭」。 – 2015-02-11 09:30:02