Monday 29 April 2013

File Upload Validation on Size or file Extension



Upload only less then 50 kb image size and Allow only jpeg, jpg, gif ,png 
Extensions type.

 <html>
<head>
<script>
function imgdym()
  {
var img = document.getElementById('filename');

var sz =img.files[0].size;
var kbsize = sz/1024
  if(kbsize > 50){ 
alert("Image is Too large! please upload less then 50 kb")
alert("your image size is: "+kbsize+" kb" );

return false;
}
else{
 var img =img.value;
 var arraydata =img.split('.');
 var Extension =arraydata[1];
if ((Extension === 'JPG') || (Extension === 'jpg')|| (Extension === 'jpeg') || (Extension === 'JPEG') || (Extension === 'GIF') || (Extension === 'gif') || (Extension === 'png') || (Extension === 'PNG'))
{
return true;
}
{
alert('Allow only jpeg, jpg, gif ,png');
return false;
}
  } 
}
</script>
</head>
<form action="upload_action.jsp" method="post" enctype="multipart/form-data" name="frmUpload" id="frmUpload">
<table align="center" width="30%" border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<td class="textn"><input type="file" name="filename" id="filename"  accept="image/gif,image/jpeg,image/tiff" class="textbox" ></td>
</tr>
<tr>
<td class="textn">  <input type="submit" name="AddFile" class="textbox1" id="AddFile" onClick="javascript:return imgdym();" value="Update Image"/> <input type="button" name="Cancel" id="Cancel" value="Cancel" class="textbox"/>
</td>
</tr>
</table>
<center><p style="color:#ff3dff;font-size:15px; font-weight:bold;">Upload User Image</p></center>
</form>


No comments:

Post a Comment