How to upload a image using file upload control?

Posted by aswinialuri-19361 under ASP.NET category on | Points: 40 | Views : 664
To upload a image using file upload control
code in .aspx page:
<h1 align="center" style="color: #FF00FF">File Upload Demo</h1>
<table align="center"><tr><td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label></td>
<td><asp:TextBox ID="TXT_NAME" runat="server"></asp:TextBox></td></tr>
<tr><td>Image:</td><td><asp:FileUpload ID="fileupload1" runat="server" /></td></tr>
<asp:Image ID="Image1" runat="server" Height="182px" Width="172px" /></td></tr>
<tr><td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /></td></tr>
<asp:Label ID="lbl5" runat="server" ></asp:Label>

code in .aspx.cs page:
write below code in click event

 protected void Button1_Click(object sender, EventArgs e)

if (fileupload1.HasFile)
if (fileupload1.PostedFile.ContentType == "image/jpeg" || fileupload1.PostedFile.ContentType == "image/gif" || fileupload1.PostedFile.ContentType == "image/x-png" && fileupload1.PostedFile.ContentLength == 1024)
string path = "~/Images/" + fileupload1.FileName;

Image1.ImageUrl = "~/Images/" + fileupload1.FileName;
Image1.Visible = true;
lbl5.Text = "<Script type='text/javascript'>alert('upload success');</script>";


i hope you understood this

Comments or Responses

Posted by: Saanjhi on: 9/28/2013 Level:Starter | Status: [Member] | Points: 10
thnx for the code :)

Login to post response