MY bLOG

Saturday, 13 July 2013

How to upload images using file upload control in asp.net

Here how to upload images usig fileupload control

In aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <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>
    </div>
    </form>
</body>
</html>

In aspx.cs page:

 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;
            }
Output:





No comments:

Post a Comment