Check File Size in FileUpload in ASP.Net C#

In previous example we have already learned to use file upload control in ASP.Net.

Here we will learn to check the file size when we upload the file using fileupload control in asp.net.

Now, we tack an example to upload image file, the file must be less than 100kb.

Here, is the layout of fileupload example.

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#

write the below code on button click events to upload file and check size of the image file.

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (FileUpload1.PostedFile.ContentLength < 102400)
{
FileUpload1.SaveAs(Server.MapPath(“~/img/”) + FileUpload1.FileName);
Label1.Text = “Image Uploaded Successfully !!”;
}
else
{
Label1.Text = “Image size must be less than 100kb”;
}
}
else
{
Label1.Text = “Select Image First !!”;
}
}

Here is the output of fileupload control exmaple.

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#

Hope this fileupload control example will help you….

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 4 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">