PDA

View Full Version : How can i get the size of the file before upload ?



debabrata
30 Nov 2011, 11:25 PM
I want to validate a file before upload using the size of file.
My code is


Ext.create('Ext.form.Panel', { title: 'Upload a Photo',
width: 400,
bodyPadding: 10,
frame: true,
renderTo: Ext.getBody(),
items: [{
xtype: 'filefield',
name: 'photo',
id:'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()){
alert(Ext.getCmp('photo').size);
}
}
}]
});


But result is always 20.
Is there any way to get the size of a file ?

icemanovich
1 Dec 2011, 2:11 AM
On server side you can see it if execute
print_r($_FILES);

structure of $_FILES array will be:

Array (
[formName] => Array (
[name] => files.sig
[type] => application/octet-stream
[tmp_name] => /tmp/phpHdk2fZ
[error] => 0
[size] => 12316
)
)


and then you can validate it like this:

if ($_FILES['formName']['size'] > SomeValue){
//success
} else {
// error
}

debabrata
1 Dec 2011, 3:46 AM
Thanks icemanovich (http://www.sencha.com/forum/member.php?287710-icemanovich) for your response.But can we check that in extjs 4 ?

icemanovich
1 Dec 2011, 4:29 AM
It seems, that in ExtJs it's can't be done. But, another way use ActiveXObject....like this:

function getSize()
{
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
alert(size + " bytes");
}

But, why you don't use PHP for this task? You can do that: click the button and in php scenario validate file size
$_FILES['formname']['size'] and then use

move_uploaded_file ($temp_file_name, $new_name)