Tutorial Extjs: Upload File and Image

Today, we will learn about how to make upload form using Extjs and php such as image (photo) upload and text file (article) upload. My opinion, upload form is very important on website, for example when saving the data to database such as guestbook and submitting article. I think this tutorial have been created by another person, but here i will try to explain it by my self, you can also look this tutorial on extjs.com. i am using combination of extjs and php.


This is the screen shot of this tutorial.
upload1

Steps of this tutorial are as folow

First step is create table in database, name of table of this tutorial is uploadf

CREATE TABLE IF NOT EXISTS `uploadf` (
`id` int(11) NOT NULL auto_increment,
`namalaporan` varchar(100) NOT NULL,
`namaFile` varchar(100) NOT NULL,
`photo` varchar(100) NOT NULL,
`date` varchar(100) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  ;

Second step is Create a php file to connection to the database, name of database of this tutorial is defafe

$nameserver = "localhost";
$username = "root";
$password = "";
$dbname = "defafe";
mysql_connect($nameserver,$username,$password)or die('error'.mysql_error());
mysql_select_db($dbname);
?>

Third step is create html file to call or load  javascript and css files





//file-upload.css and FileUploadField can be found here ext-2.2\examples\form


After that create a javascript file to make  upload form, the fields of this form are Filename, Fileconten, photo, and date.  The most important of this script is “xtype: ‘fileuploadfield’,”

Ext.onReady(function(){

Ext.QuickTips.init();
var fp = new Ext.FormPanel({
fileUpload: true,
width: 320,
frame: true,
//height:170,
autoheight:true,
bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 60,
defaults: {
anchor: '95%',
//allowBlank: false,
msgTarget: 'side'
},
items: [

{
xtype: 'textfield',
name: 'Filename',
fieldLabel: 'File Name '
},{
xtype: 'fileuploadfield',
id: 'form-file',
emptyText: 'Select an file',
fieldLabel: 'File',
name: 'Fileconten',
buttonCfg: {
text: '',
iconCls: 'upload-icon'
}
},
{
xtype: 'fileuploadfield',
id: 'form-file1',
emptyText: 'Select an image',
fieldLabel: 'Image',
name: 'photo',
buttonCfg: {
text: '',
iconCls: 'upload-icon' //usuing icon on  browse button.
}
},
{
xtype: 'datefield',
fieldLabel: 'Date',
anchor: '95%',
name: 'date',
format: 'm/d/Y'
}
],
buttons: [{
text: 'Save',
handler: function(){
if(fp.getForm().isValid()){
fp.getForm().submit({
url: 'phpupload.php',
waitMsg: 'Uploading your photo...',
success: function(fp, o){ //if success
Ext.MessageBox.alert('Creation OK','uplaoding data success..');

},
failure: function(fp, o){ //if not success
Ext.MessageBox.alert('Warning','failure uploading data...');
}
});
}
}
},{
text: 'Reset',
handler: function(){
fp.getForm().reset();
}
}]
});

var createwindow = new Ext.Window({

title:'Form Upload',
width:315,
height:190,
closable: false,
items: fp
});
createwindow.show();

});

Finally create php file to process data, to post to the databse, the image types  of this tutorial are gif, jpeg, x-png.

include "koneksi.php";
$Filename = $_POST['Filename'];
$Fileconten= $_FILES['Fileconten']['name'];
$Photoname = $_FILES['photo']['name'];
$date = $_POST['date'];

if ($_FILES['photo']['type'] == "image/gif" || $_FILES['photo']['type'] == "image/jpeg" || $_FILES['photo']['type'] == "image/x-png")
{
move_uploaded_file ($_FILES['photo']['tmp_name'],"image/".$_FILES['photo']['name']);
move_uploaded_file ($_FILES['Fileconten']['tmp_name'],"artikel/".$_FILES['Fileconten']['name']);
$sql_query= mysql_query("INSERT INTO uploadF (`id`,`Filename`,`Fileconten`,`photo`,`date`) VALUES (null,'$Filename','$Fileconten','$Photoname',now())");
if ($sql_query)
{
echo '{"success":"true"}';
}
else
{
echo '{"success":"false"}';
}
}
?>

Download Source code

Tags: , , , , , , , , , , , , , , , , , , , , , , , ,

Incoming search terms for the article:

extjs file upload , extjs upload , extjs upload file , Extjs fileupload , extjs fileuploadfield , file upload extjs , extjs image upload , extjs file upload example , fileuploadfield extjs , extjs upload image , upload extjs , extjs file upload php , upload image extjs , ext file upload , ext js file upload , extjs fileupload example , file upload in extjs , extjs file browser , upload file extjs , extjs multiple file upload , ext js upload , extjs upload form , extjs file uploader , extjs upload php , photo upload in EXTJS , extjs form file upload php , extjs 3 upload , php photo upload tutorial , extjs form file upload , extjs upload example , extjs upload file example , XML tag name mismatch (expected img) , extjs php file upload , extjs ajax file upload , extjs uploadfile , extjs File Upload Field , extjs 3 file upload , extjs image in form , image upload extjs , extjs file upload sample , file upload using extjs , upload file with ext js , extjs image button , extjs image , Upload Files ExtJS , extjs buttonCfg , extjs upload files , extjs upload button , extjs form upload image , extjs user file file upload php extjs themes , extjs file upload , extjs theme , extjs upload , extjs upload file , extjs grid checkbox , extjs tutorial , extjs login , extjs login form , extjs grid paging exjs file upload , getForm() load({url:simplegrid php?act=get&id= m[0] get(id) waitMsg:Loading}); , extjs get grid row height , submit grid checkbox extjs , extjs grid checkbox submit , extJS login demo not work in 3 1 , upload xls file using extjs , extjs CheckboxSelectionModel color , extjs form load callback , upload file extjs tutorial 

15 Responses to “Tutorial Extjs: Upload File and Image”

  1. Borut says:

    Thank you for sharing this nice example. But there seems to be a problem in IE (8). When I select a file, default text in input box “Select a file” still remains there. And After clicking “Save” I get javascript error, saying “Object doesn’t support this property or method”.

  2. Abdel says:

    hi,
    Thanks for the post. I tried out your exmaple.. but i have some trouble! i can see two browse buttons and text fields. one is rendered using ExtJs CSS and other is plain HTML’s browse control! :-( how do i resolve this?

    Thanks in advance,
    Abdel Olakara

  3. Satya Alluri says:

    @Abdel
    I was stuck with the same problem. Importing file-upload.css from the examples/form folder solves the problem.

  4. Satya Alluri says:

    @Abdel
    I was stuck with the same problem. Importing the file file-upload.css from the examples/form folder solves the problem.

  5. Agus Akhmad says:

    mas.. kok saia coba di server gagal yah :( .. klo do lokal malah berhasil

  6. Valentina says:

    Hi, I’ve tried your example out, I’ve only 1 upload field more. I cann upload 2 different files or every file only then it works, but when I upload all 3 files it dosen’t upload the files anymore. I don’t have any error. Later I’ve tried this with 4 uploadfiles and the same is happen, when I upload 3 files it works and all files alone it works too but when I would upload all 4 files it dosn’t work?

    Do you have any advise? Do you know anything about that?

    Best regards

    Valentina

  7. Jonathan Lee says:

    Hello.
    When I ran this after I downloaded, $_POST and $_FILES didn’t contain any value of ‘Filename’, ‘photo’, ‘Fileconten’, ‘date’ in phpupload.php. Do I need to configure in php first?

  8. faisal says:

    masak sich??????

  9. Christof says:

    “hanks for the post. I tried out your exmaple.. but i have some trouble! i can see two browse buttons and text fields. one is rendered using ExtJs CSS and other is plain HTML’s browse control! how do i resolve this?”

    You must include the css file.

  10. veri says:

    @Jonathan Lee
    cek name field must be match with insert statement in php code…

  11. veri says:

    veri :
    @Jonathan Lee
    check field name must be match with insert statement in php code…

  12. Julius says:

    Can you provide a demo of your upload image

  13. Benedict Aluan says:

    Hi,

    Just want to ask how can you specify the file types (mime types) to upload in the Fileupload component.

    Thanks.

  14. manu says:

    hi,

    i’ve embedded the script in my application and it works great :)

    but my problem is, if i do not add any attachments i get the following error

    XML tag name mismatch (expected img)

    has someone the same prob ?

  15. Pay attention when checking “file type”, because IE send image/pjpeg for progressive jpeg. So this mime type has to be added.

Leave a Reply

Security Code: