2015-02-27 1 views
1
// Here's my extjs code 
    Ext.define('ArticulationAgreement.view.Filechooser',{ 
     extend : 'Ext.form.Panel', 
     alias : 'widget.filechooser', 
     mixins: ['Deft.mixin.Injectable', 'Deft.mixin.Controllable'], 
     //controller : ['ArticulationAgreement.controller.FilechooserController'], 
     width : 1500, 
     height : 300, 
     bodyPadding: '10 10 0', 
     frame: true, 

     initComponent : function(){ 
      var me = this; 
      Ext.apply(me, { 


       title : 'Add the CSV Files', 
       defaults: 
          { 
           anchor: '30%', 
           allowBlank: false, 
           msgTarget: 'side', 
           labelWidth: 100 
          }, 
       items : [ 
         { 
          xtype: 'filefield', 
          id: 'form-file', 
          emptyText: 'Select a File', 
          //maxLength : '30', 
          fieldLabel: 'File Selected', 
          name: 'fpath', 
          buttonText: 'upload file' 

         }, 

        { 
          text : 'Save File', 
          xtype : 'button', 
          width: 75, 
          itemId : 'save', 
          layout : 'hbox', 
          anchor : '5%', 
          handler : function(){ 
          //console.log(this.up('form').down('textfield[name=file-path]').value); 
          var form = this.up('form').getForm(); 
          if(form.isValid()){ 
           form.submit({ 
           url : 'http://localhost:8080/ssp/api/upload.action', 
           waitMsg: 'Uploading your file', 
           success: function(fp, o) { 
            Ext.Msg.alert('Success', 'Your file has been uploaded.'+fp.responseText); 
           } 
           }); 
          } 
          } 
        }, 
        { 
         text : 'Reset', 
         xtype : 'button', 
         width : 75, 
         layout : 'hbox', 
         anchor : '5%', 
         handler : function(){ 
          this.up('form').getForm().reset(); 
         } 
        } 
       ] 
      }); 
      return me.callParent(arguments); 
     } 
    }); 

// Вот моя весна код контроллераЯ не был в состоянии прочитать содержимое файла в Java коде, пропускают через ExtJS кода Java яровой контроллер

@RequestMapping(method = RequestMethod.POST) 
public 
    ResponseEntity<String> uploadFile(FileUploadBean uploadItem, BindingResult result) throws Exception { 
     System.out.println("QUERY TO UPLOAD FILE"); 
     try { 

       MultipartFile file = uploadItem.getFile(); 
       String fileName = null; 
       InputStream inputStream = null; 
       OutputStream outputStream = null; 
       //Exception on next line: 
       System.out.println("Test upload: " + uploadItem.getFile().getOriginalFilename()); 

      //rest of the code 

      return null; 
     } 

// Я получаю исключение в java controller, и он дает мне ответ в коде extjs as- // Uncaught Ext.JSON.decode(): Вы пытаетесь декодировать недопустимую строку JSON: //, пожалуйста, помогите мне, если это возможно, заблаговременно.

ответ

0
  //Extjs Code 
      { 
        xtype: 'filefield', 
        emptyText: 'Select a File', 
        //maxLength : '30', 
        fieldLabel: 'File Selected', 
        name: 'file',//change in only this line was needed 
        buttonText: 'Choose File' 
      } 

      //Spring Controller Code 
      @RequestMapping(method = RequestMethod.POST) 
      public @ResponseBody String uploadFile(FileUploadBean uploadItem, BindingResult result) throws Exception { 
    System.out.println("QUERY TO UPLOAD FILE"); 
    try { 

      CommonsMultipartFile file = uploadItem.getFile();//changed line 
      String fileName = null; 
      InputStream inputStream = null; 
      OutputStream outputStream = null; 

      System.out.println("Test upload: " + uploadItem.getFile().getOriginalFilename()); 

     //rest of the code 
     } 
     return null; 
    } 
+0

Не могли бы вы подробнее рассказать о своем решении, добавив немного подробного описания вашего решения? – abarisone

+0

Согласно моему опыту - при загрузке файла с использованием extjs и spring атрибут name в коде extjs влияет на большинство, и он должен быть «файлом», а многие говорят, что имя атрибута класса FileUploadBean в java должно быть таким же, как и у extjs filefield, но я не думаю, что это может быть правдой, поскольку ранее я использовал другое имя, а не «файл» с синхронным атрибуту FileUploadBean, я также изменил MultipartFile на CommonsMultipartFile, и весь код работает нормально (я редактировал код java). –