Steps to create File Upload field using Ext Js
4 Answers
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!

- 69,876
- 20
- 132
- 137
-
Did you try the example you places here? – Eugene Aug 26 '10 at 14:25
-
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? – SW4 Aug 26 '10 at 15:34
-
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 Aug 26 '10 at 22:05
-
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 – SW4 Aug 27 '10 at 15:53
-
1@ErgoSummary please include also the part where the json format is returned after inserting the image in db. – tonyo Jul 22 '13 at 09:22
-
what determines that it goes through : success: function(form,action){..} ? I am stuck on this. Is it just success=true in the response JSON? – Oliver Watkins Nov 04 '14 at 13:13
-
Note the answer is 4yrs old so may only be relevant for 3.2x – SW4 Nov 04 '14 at 13:34
If you look at the examples available at www.ExtJS.com, you'll find this one.
Although it is based on the standard HTML file upload - just like this answer suggests.
setting the id only will result in the $_FILES array name to be the same as the id. If you need to use something else then set the name config option and it will use that instead.

- 246
- 1
- 3