【问题标题】:File upload in extjs 4.2 without form.submit()在没有 form.submit() 的 extjs 4.2 中上传文件
【发布时间】:2013-06-06 14:49:02
【问题描述】:

我正在尝试在 extjs 中上传文件(截至目前任何扩展名)。我有一个模型和商店。文件上传是从窗口发生的,我在窗口中没有表单。我在网上尝试的所有示例都使用 form.submit()。我改为使用下面的 Ajax 调用将数据发送到服务器。

Ext.Ajax.request({

            url : 'qaf/saveSetupDetails.action',

            params : {
                'data' : recordsToSend
            },
            failure : function(response){
                //console.log('error connecting controller');
            },
            success : function(response){
                //console.log('successfully submitted');
            }
        });

数据中要发送的记录如下。

var store = Ext.getStore('SomeStore');
        var modifiedRecords = store.getModifiedRecords();
        var recordsToSend = [];
        if(modifiedRecords.length > 0){
            Ext.each(modifiedRecords, function(record){
                recordsToSend.push(record.data);//I'm sure that this is so dump but this is how I do it for other records which are string and not sure how to do it for a file...
            });
        }
        Ext.USE_NATIVE_JSON = true;
        recordsToSend = Ext.encode(recordsToSend);

在模型中设置记录时,我使用下面的代码..

var rec = Ext.create('QAF.model.MyModel');
rec.set('modelField',Ext.getCmp('fileUploadCompID').value);

我收到了一个 500 状态错误的响应 "Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile]"

我确定这是因为我将值设置为模型的方式,因为Ext.getCmp('fileUploadCompID').value 给出了文件名。请告诉我如何将文件设置为模型以及我必须为模型中的字段指定什么数据类型。

下面是我如何尝试在 spring 控制器中检索文件。

@RequestMapping (value = "/qaf/saveSetupDetails.action")
    public @ResponseBody
    void saveSetupDetails(@RequestParam CommonsMultipartFile data)throws Exception{
        log.info("Enter into saveSetupDetails method..." + data.getOriginalFilename());
    }

请让我知道我做错了什么以及必须做些什么来解决这个问题......

【问题讨论】:

    标签: extjs4.2


    【解决方案1】:

    如果您仍想使用 ExtJS 的 fileuploadfield 并使用 HTML5 FileReader 通过 AJAX 调用上传,您可以这样做:

    launchUpload: function () {
        //get a handle of the "file" input in the widget itself...
        var fileInput = document.getElementById(yourUploadField.button.fileInputEl.id);
        var fileReader = New FileReader();
        var fileToUpload = fileInput.files[0]; //assuming your only uploading one file...
        var me = this
    
        fileReader.onload = function (e) {
             me.onLoadFile(e, me, fileToUpload.name);
        }
    
        fileReader.readAsDataURL(fileToUpload);
    
    }, 
    onLoadFile: function (e, scope, filename) {
    
         //I carry the scope around for functionality...
    
         Ext.Ajax.request({
            method: 'POST',
            url: 'url',
            scope: scope,
            jsonData: { fileNameParameter: filename, fileDatainBase64: e.target.result},
            success: function (response, operation) {
                //success..
            },
            failure: function (response, operation) {
                //failure...
            }
        });       
    
    }
    

    【讨论】:

      【解决方案2】:

      是的,您可以使用 Ajax 和 FormData API:

      var file = s.fileInputEl.dom.files[0],
           data = new FormData();
      data.append('file', file);
      Ext.Ajax.request({
         url: '/upload/files',
         rawData: data,
         headers: {'Content-Type':null}, //to use content type of FormData
         success: function(response){
         }
      });
      

      看我的演示here

      【讨论】:

        【解决方案3】:

        Extjs 的 filefield 是不行的

        Extjs 的文件字段从选择文件中返回字符串 url。

        我认为您需要在更改事件中选择的文件,但文件字段没有此事件

        您可以使用此解决方案, 也许你从解决方案中得到一个想法

        示例:http://jsfiddle.net/e3M3e/e8V7g/

        var itemFile = null;
        Ext.create('Ext.panel.Panel', {
            title: 'Hello',
            width: 400,
            html: "<input id='inputFile' type='file' name='uploaded'/>",
            renderTo: Ext.getBody(),
            listeners: {
                afterrender: function() {
                    itemFile = document.getElementById("inputFile");            
                    itemFile.addEventListener('change', EventChange, false);
                }
           }
        });
        
        function EventChange(e){    
            var files = itemFile.files;
            console.log(files);
        }
        

        【讨论】:

          【解决方案4】:

          ExtJs 版本 6.0.1 - 使用 iframe

          Ext.define('xxx.yyy.UploadData', {
              extend : 'Ext.form.Panel',
              alias  : 'widget.uploaddata',
          
              initComponent : function(){        
                  var me = this;        
          
                  me.items = [{
                    xtype      : 'filefield',
                    margin     : '20 0 0 20',
                    name       : 'excelfile',
                    fieldLabel : 'Choose file',
                    msgTarget  : 'side',
                    allowBlank : false,
                    anchor     : '30%',
                    buttonText : 'Select',
                    defaultButtonTarget : 'fileframe'
                  },{
                    xtype : 'panel',
                    html  : '<iframe width="340" height="340" style="display: none" name="fileframe"></iframe>'
                  },{
                    xtype : 'button',
                    text  : 'Import',
                    handler : function(){
                      var form = this.up('form').getForm();
                      if(form.isValid()){
                          form.submit({
                              url     : './upload.php',
                              waitMsg : 'uploading...',
                              success : function(fp, o) {
                                  alert("OK");                            
                              }
                          });
                      }
                  }
              }];
          
              me.callParent();        
              }    
             });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多