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>
<tr><td>
<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>
</table>
<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;
fileupload1.SaveAs(Server.MapPath(path));
Image1.ImageUrl = "~/Images/" + fileupload1.FileName;
Image1.Visible = true;
lbl5.Text = "<Script type='text/javascript'>alert('upload success');</script>";
}
}
}
i hope you understood this