ASP.NET - fileupload to check image size when uploaded

Asked By var char on 29-Apr-15 07:59 AM
i need to check the passport photo size when image is loaded.what is the maximum size i have to give.
how to validate wheather user is uploading not more than the size what we specify.
Jitendra Faye replied to var char on 29-Apr-15 08:00 AM

Use this function -

<img id="myImage" src="" style="display:none;"><br>
<button onclick="findSize();">Image Size</button>
<input type="file" id="loadfile" />
<input type="button" value="find size" onclick="findSize()" />
<script type="text/javascript">

function findSize() { if ( $.browser.msie ) { var a = document.getElementById('loadfile').value; $('#myImage').attr('src',a); var imgbytes = document.getElementById('myImage').fileSize; var imgkbytes = Math.round(parseInt(imgbytes)/1024); alert(imgkbytes+' KB'); }else { var fileInput = $("#loadfile")[0]; var imgbytes = fileInput.files[0].fileSize; // Size returned in bytes. var imgkbytes = Math.round(parseInt(imgbytes)/1024); alert(imgkbytes+' KB'); } } </script>
Riley K replied to var char on 29-Apr-15 08:00 AM
Before uploading calculate the size of fileuploaded.  If it is more than certain KB then deny to upload, here is the code for that:

protected void custValid_ServerValidate(object source, ServerValidateEventArgs args)
long contentLen = fleupldAddPhoto.PostedFile.ContentLength;
double LengthConvert = Convert.ToDouble(contentLen / 50000);
string Extension = default(string);
if (fleupldAddPhoto.PostedFile.FileName.IndexOf('.') > 0)
Extension = fleupldAddPhoto.PostedFile.FileName.Substring(fleupldAddPhoto.PostedFile.FileName.LastIndexOf('.')).ToLower();
if (!(Extension == ".jpg"))
args.IsValid = false;
custValid.ErrorMessage = "Please Upload Only .JPG Images.";
else if (LengthConvert > 1)
args.IsValid = false;
custValid.ErrorMessage = "Image Size should be less than 50 KB.";
args.IsValid = true;

Try this and let me know
Ravi S replied to var char on 29-Apr-15 08:00 AM
You can try to set the max size at your web.config file.
 Take a look at "maxRequestLength":
Reena Jain replied to var char on 06-Jul-11 09:38 AM

if your intention is to restrict file to upload then you need to do check file size  before upload, for that you can use javascript
<script language=”javascript” type=”text/javascript”>
  var objFSO ;
  objFSO = new ActiveXObject(“Scripting.FileSystemObject”);
  function CheckExtention(el) // pass Upload File object’s ClientID here
  var myel= document.getElementById(el);
  var file;
  var path = myel.value;
  file = objFSO.getFile(path);
  var size;
  size = file.size ; // This size will be in Bytes
  // We are converting it to KB as below
alert('File Size is : '+ file.size /1024 +' KB');


try above code and let me know
Reena Jain replied to var char on 29-Apr-15 08:00 AM
By default, the maximum size of a file to be uploaded to a server using the ASP.NET FileUpload control is 4MB. You cannot upload anything that is larger than this limit.

To change this size limit, you have to make some changes in the application's web.config:

  <httpRuntime  maxRequestLength="102400" executionTimeout="360"/>

maxRequestLength - Attribute limits the file upload size for ASP.NET application. This limit can be used to prevent denial of service attacks (DOS) caused by users posting large files to the server. The size specified is in kilobytes. As mentioned earlier, the default is "4096" (4 MB). Max value is "1048576" (1 GB) for .NET Framework 1.0/1.1 and "2097151" (2 GB) for .NET Framework 2.0.

- Attribute indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by the application. The executionTimeout value should always be longer than the amount of time that the upload process can take.
Kirtan Patel replied to var char on 06-Jul-11 10:34 PM
you can use validations to check the file size in uploading image

<asp:CustomValidator ID="valFileSize" runat="server" 
  ErrorMessage="The image exceeds 10 MB" 
  onservervalidate="valFileSize_ServerValidate" Display="Dynamic" />
protected void valFileSize_ServerValidate(object source, ServerValidateEventArgs args)
  if (IsValid)
    int maxSize = 5 * 1024 * 1024;
    if (fileImage.PostedFile.ContentLength > maxSize)
      args.IsValid = false;