Category Archives: FileUpload

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

 

 

FileUpload Control in ASP.Net C#

Hi, Today we will learn how to use fileupload control in ASP.Net with C#.

FileUpload Control is used to upload / save file to server or local drive.

If we save or upload any file like image, text, zip we can use fileupload contol SaveAs method to upload file to disk.

Here we tack an example to understand save image file in our project.

First create asp.net web application and create a new folder named img to save image file in it.

here is the HTML code for design web page.

<table align=”center” class=”style6″>
<tr>
<td style=”text-align: right”>
Select <span lang=”en-us”>File</span> :</td>
<td style=”text-align: left”>
<asp:FileUpload ID=”FileUpload1″ runat=”server” />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td style=”text-align: left”>
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click”
style=”font-weight: 700″ Text=”UPLOAD” />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td style=”text-align: left”>
<asp:Label ID=”Label1″ runat=”server”></asp:Label>
</td>
</tr>
</table>

Here is the layout of asp.net web page for fileupload control example.

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#

Now, write the below code in Button click event for save / upload file to pre defined location.

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath(“~/img/”) + FileUpload1.FileName);
Label1.Text = “Image Uploaded Successfully !!”;
}
else
{
Label1.Text = “Select Image First !!”;
}
}

Here, above we write code for save image file to img folder in our project.

– Server.MapPath : Define full path of our web application

– HasFile : Check whether Fileupload  control has select file or Not.

Here is some out put image of fileupload control example in asp.net

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#

 

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#

FileUpload contorl in ASP.Net with C#

FileUpload contorl in ASP.Net with C#