ASP.NET - fileupload - Asked By var char on 06-Jul-11 09:03 AM

hi,

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.






thanks 
Jitendra Faye replied to var char on 06-Jul-11 09:14 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>


Hope this will help you.
Riley K replied to var char on 06-Jul-11 09:24 AM
Hi, 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.";
}
else
{
args.IsValid = true;
}
}

Try this and let me know
Ravi S replied to var char on 06-Jul-11 09:29 AM
HI

You can try to set the max size at your web.config file.
 Take a look at "maxRequestLength":

  http://www.element-it.com/OnlineHelp/Webconfig.html
Reena Jain replied to var char on 06-Jul-11 09:38 AM
hi,

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
code
<script language=”javascript” type=”text/javascript”>
  var objFSO ;
  try
  {
  objFSO = new ActiveXObject(“Scripting.FileSystemObject”);
  }
  catch(err)
  {
  alert(err.message);
  alert(err.number);
  }
  
  function CheckExtention(el) // pass Upload File object’s ClientID here
  {
  var myel= document.getElementById(el);
  var file;
  var path = myel.value;
  //alert(path);
  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');

  }
</script>

try above code and let me know
Reena Jain replied to var char on 06-Jul-11 09:40 AM
Hi,

I get one more solution To share with you

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:

<system.web>
  <httpRuntime  maxRequestLength="102400" executionTimeout="360"/>
</system.web>

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.

executionTimeout
- 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.

Hope this will help you
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;
    }
  }
}