Extjs - upload file using filefield
Asked Answered
H

3

6

My extjs code like http://www.objis.com/formationextjs/lib/extjs-4.0.0/docs/api/Ext.form.field.File.html

Ext.create('Ext.form.Panel', {
    title: 'Upload a Photo',
    width: 400,
    bodyPadding: 10,
    frame: true,
    renderTo: Ext.getBody(),    
    items: [{
        xtype: 'filefield',
        name: 'photo',
        fieldLabel: 'Photo',
        labelWidth: 50,
        msgTarget: 'side',
        allowBlank: false,
        anchor: '100%',
        buttonText: 'Select Photo...'
    }],

    buttons: [{
        text: 'Upload',
        handler: function() {
            var form = this.up('form').getForm();
            if(form.isValid()){
                form.submit({
                    url: 'photo-upload.php',
                    waitMsg: 'Uploading your photo...',
                    success: function(fp, o) {
                        Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
                    }
                });
            }
        }
    }]
});

my photo-upload.php file

echo "{success:true}";

But when success o.result.file show undefined. I think it will show file name after success.
How can i make it show file name after success thanks

Horacehoracio answered 20/6, 2013 at 8:27 Comment(0)
A
1

You are just returning "{success:true}" which is not even valid JSON but expect ExtJS to provide you with even more data?

Try to return JSON like

{ "success": true, "file": "filename" }

so that there is a file property in the result that the client can read.

Arborvitae answered 20/6, 2013 at 8:40 Comment(1)
I did this: $result = array( 'success' => 'true', 'file' => 'filename.ext' ); $x= json_encode($result); echo $x;Glisson
H
0

just return proper json construction to eval in front end.

To display the file name as a message, u dont need to return value from backend, already it available with you. refer below code

var fileUploadComp = // eg: Ext.getCmp('DictUploadField'); var fileName = fileUploadComp.getValue();

place this fileName in your message.

Thanks.

Hypophyge answered 20/6, 2013 at 9:2 Comment(0)
T
0

​Note: server response type should be "text/html".

return Json(new {
    success = true,
    msg = "Your file has been uploaded"
}, "text/html");

Live demo is here

Togetherness answered 20/2, 2014 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.