Search code examples
extjsextjs4extjs5extjs6extjs6-classic

ExtJS 6 - How to upload a file without using form?


Ext JS provides fileuploadfield which is bundled with a button to browse local files. I just need to upload a file using as soon as it is selected from local instead of using a submit button in order to trigger the post process. Could not find an event which is fired after the file is selected.

p.s. Actually, the version I use is Ext JS 6 but I think the solutions based on previous versions do the work as well.


Solution

  • You will need to use a form if you want to submit the file. Even if you want the button to be in a toolbar, you can enclose it in a form and it will still look like a normal toolbar button (you will need to specify the proper ui config for this).

    Example:

    dockedItems: [{
        dock: 'top',
        xtype: 'toolbar',
        items: [{
            xtype: 'form',
            padding: '10 0 0',
            url: 'submit/image',
            items: {
                xtype: 'filefield',
                buttonOnly: true,
                width: 100,
                buttonConfig: {
                    text: 'Add logo',
                    width: '100%',
                    ui: 'default-toolbar-small'
                },
                listeners: {
                    change: function (filefield) {
                        filefield.up('form').submit();
                    }
                }
            }
        }, {
            text: 'Remove logo'
        }, '-', {
            text: 'Discard changes'
        }]
    }]
    

    Working fiddle example: https://fiddle.sencha.com/#view/editor&fiddle/1pdk