Image Uploading in ASP.NET

FileUpload.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUpload.aspx.cs" Inherits="WebApplication8.FileUpload" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fu" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:Label ID="imgName" runat="server" Text=""></asp:Label>
        <asp:Image ID="img" runat="server" Width="50" Height="50" />
    </div>
    </form>
</body>
</html>


FileUpload.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
        {

            if(fu.HasFile)
            {
                if(fu.PostedFile.ContentType=="image/jpeg")
                {
                        if(fu.PostedFile.ContentLength<100240)
                    {
                        string filename = Path.GetFileName(fu.FileName);
                        string path= "~/images/" +filename;
                        fu.SaveAs(Server.MapPath(path));
                                                }
                    }
                    else
                    {
                        Label1.Text = "File Size Should be Less Than 100 kb";
                    }
                }
                else
                {
                    Label1.Text = "Only Jpeg Files are allowed";
                }
               
            }

Comments

Popular posts from this blog

How to Delete Data from Database Using ASP.NET

Login Form with Database in ASP.NET