File upload in extjs 4.2 without form.submit()

余生长醉 提交于 2019-12-04 05:33:36

You can't do it with filefield of Extjs

filefield of Extjs return string url from select file.

I think you need the file selected as occurs in change event but filefield haven't this event

you can use this solution, maybe you get one idea from the solution

Example: 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);
}

If you want to still use ExtJS's fileuploadfield and upload through an AJAX call using HTML5 FileReader, you can do it like such:

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...
        }
    });       

}

ExtJs version 6.0.1 - Using 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();        
    }    
   });

Yes, you can use Ajax and 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){
   }
});

See my demo here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!