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#

 

Leave a Reply

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

6 + 6 =


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="">