tags:

views:

2105

answers:

4

Steps to create File Upload field using Ext Js

A: 

Why would you bother? Just use the HTML file input tag:

<form method="post" enctype="multipart/form-data">
<input type="file" name="myfile" />
</form>
Charlie Somerville
i need it for EXTJS
brindha
Why do you need to use a JS library for something that vanilla HTML can do?
Charlie Somerville
Because a) the field will be styled like other Ext fields, b) it will participate in Ext layouts like other fields, and c) it provides Ext-specific event callbacks, e.g. to notify when upload is done.
bmoeskau
+1  A: 

If you look at the examples available at www.ExtJS.com, you'll find this one:

link text

Although it is based on the standard HTML file upload - just like Charlie suggests.

Chau
A: 

Try this one: http://www.sencha.com/learn/Extension:UploadForm

stefanm
+2  A: 

As far as specific steps are concerned, using functionality supported in ExtJS 3x, your best best is to use this module/plugin:

http://dev.sencha.com/deploy/dev/examples/form/file-upload.html

The core script comes with the Ext JS package, in your main HTML file (where you have linked to the core Ext scripts), in the head section after your other scripts put:

<script type="text/javascript" src="nameofyourextfolder/examples/ux/fileuploadfield/FileUploadField.js"></script>

Sadly, there isnt a huge amount of documentation on this element of Ext JS- however for basic functionality, you can create a form with an async upload field using the below:

            myuploadform= new Ext.FormPanel({
                fileUpload: true,
                width: 500,
                autoHeight: true,
                bodyStyle: 'padding: 10px 10px 10px 10px;',
                labelWidth: 50,
                defaults: {
                    anchor: '95%',
                    allowBlank: false,
                    msgTarget: 'side'
                },
                items:[
                {
                    xtype: 'fileuploadfield',
                    id: 'filedata',
                    emptyText: 'Select a document to upload...',
                    fieldLabel: 'File',
                    buttonText: 'Browse'
                }],
                buttons: [{
                    text: 'Upload',
                    handler: function(){
                        if(myuploadform.getForm().isValid()){
                            form_action=1;
                            myuploadform.getForm().submit({
                                url: 'handleupload.php',
                                waitMsg: 'Uploading file...',
                                success: function(form,action){
                                    msg('Success', 'Processed file on the server');
                                }
                            });
                        }
                    }
                }]
            })

What this code will do is create a new formpanel with an upload field and an upload button. When you click the upload button- the selected file will be sent to the serverside script handleupload.php (or whatever you call it). It is then this script that handles what you want to do with the file. An example of this could potentially be:

    $fileName = $_FILES['filedata']['name'];
    $tmpName  = $_FILES['filedata']['tmp_name'];
    $fileSize = $_FILES['filedata']['size'];
    $fileType = $_FILES['filedata']['type'];
    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);
    if(!get_magic_quotes_gpc()){
        $fileName = addslashes($fileName);
    }
    $query = "INSERT INTO yourdatabasetable (`name`, `size`, `type`, `file`) VALUES ('".$fileName."','".$fileSize."', '".$fileType."', '".$content."')";
    mysql_query($query); 

Which would inject the file into a SQL DB. The thing to remember is the server side file handles an upload just as a normal HTML form would...

Hope this helps!

Ergo Summary
Did you try the example you places here?
Eugene
Sure- I have it in working implementations... you will need to make some tweaks for your code but it definitely works. Which part are you having trouble with?
Ergo Summary
The part where items object `xtype : 'fileuploadfield'` have to have option `name : 'filedata'`. Since this option is used in `$_FILES` array, not `id : 'filedata'`.
Eugene
Could be added for good practive, but using Ext JS you can get away with just setting id, it defaults to that when sending the request if no name is set
Ergo Summary