Check Uploaded File Size And Type JQuery
We upload files in web applications, sometimes we require to get uploaded file size and type at client side. I am sharing my JavaScript code to get uploaded file size and type at client side.
jQuery("input[name='member-file']").change(function () {
var type = '';
var iSize = 0;
var error = true;
if (jQuery.browser.msie) {
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var sPath = jQuery(this)[0].value;
var objFile = objFSO.getFile(sPath);
iSize = objFile.size;
iSize = iSize / 1024;
type = objFile.type;
} else {
iSize = (jQuery(this)[0].files[0].size / 1024);
type = jQuery(this)[0].files[0].type;
}
var TypeArr = [
'video/mp4',
'image/jpg',
'image/jpeg',
'image/gif',
'image/png'
];
jQuery(TypeArr).each(function (i, l) {
if (type == l) {
error = false;
return false;
}
});
if (error == false) {
if (iSize > 0) {
iSize = (Math.round((iSize / 1024) * 100) / 100)
}
if (iSize > 10) {
error = true;
}
}
if (error == true) {
// show error
return false;
} else {
// no error
return true;
}
});
No comments:
Post a Comment