Click here to Skip to main content
15,071,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i have web project,that accept user details and save that details in sql server table.
i want that when user upload profile photo then server check the photo size and reply the user that he has upload only 4 kb photo.

photo upload code is ....

page design code

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PhotoPanel.aspx.cs" Inherits="PhotoPanel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font: normal 12px/15px verdana;
            background-color: #f1f5fa;
        }
        .w_button
        {
            border: 1px solid #135C86;
            background: -webkit-gradient( linear, left top, left bottom, color-stop(0.1, #7EC5E2), color-stop(0.9, #3692C6) );
            background: -moz-linear-gradient( center top, #7EC5E2 10%, #3692C6 90% );
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#7EC5E2', endColorstr='#3692C6');
            background-color: #7EC5E2;
            -moz-box-shadow: inset 0px 1px 2px 0px #FFFFFF;
            -webkit-box-shadow: inset 0px 1px  2px 0px #FFFFFF;
            box-shadow: inset 0px 1px  2px 0px #FFFFFF;
            -moz-border-radius: 4px;
            -webkit-border-radius: 4px;
            border-radius: 4px;
            text-shadow: 1px 1px 2px #5a5b5b;
            font-weight: bold;
            margin: 5px 5px;
            padding: 6px;
            color: #FFFFFF;
            letter-spacing: 0.5px;
            font-family: 'Arial', sans-serif;
            font-size: 16px;
            width: 100%;
            text-transform: capitalize;
            text-align: center;
            text-decoration: none;
            cursor: pointer;
            display: inline-block;
        }
        #pp_main
        {
            width: 375px;
            color:#414141;
            padding: 5px 10px;
            background-color: #f1f5fa;
        }
        .pp_cp
        {
            background: none repeat scroll 0 0 #09458D;
            color: #FFFFFF;
            font-weight: bold;
            margin-bottom: 10px;
            padding: 3px;
            text-align: center;
            text-transform: uppercase;
        }
        .pp_img
        {
            width: auto;
            height: auto;
            float:left;
        }
        .pp_fileup_button
        {
            float:left;
            margin-left: 7px;
        }
        .pp_name
        {
            margin: 10px 0 20px;
        }
        .name
        {
            text-transform: capitalize; font:bold 17px/17px verdana; color:#000;
        }
        .pp_fileup
        {
            margin: 5px 0;
        }
        .pp_button
        {
            margin: 10px 0;
        }
        .clr
        {
            clear:both;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="pp_main">
        <div class="pp_cp">
            <span>Change Your Profile Pic</span>
        </div>
        <div>
            <div class="pp_img">
                <asp:Image ID="Image1" CssClass="pc" runat="server" Width="120" Height="120" ImageUrl="~/images/Avatar.jpg"
                    alt="avatar" />
            </div>
            <div class="pp_fileup_button">
                <div class="pp_name">
                    <asp:Label ID="Label6" runat="server" class="name"></asp:Label>
                </div>
                <div class="pp_fileup">
                    <asp:FileUpload runat="server" ID="flImage1"></asp:FileUpload>
                </div>
                <div class="pp_button">
                    <%--<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/save.png"
                        onclick="ImageButton1_Click" />--%>
                    <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" class="w_button" />
                </div>
            </div>
            <div class="clr"></div>
            <asp:Label ID="Label1" runat="server" ForeColor="White" Font-Bold="True" Font-Italic="True"
                Font-Size="Large" Visible="false"></asp:Label>
        </div>

    </div>
    </form>
</body>
</html>




Code file
C#
public partial class PhotoPanel : System.Web.UI.Page
{
    String constr = Convert.ToString(ConfigurationManager.ConnectionStrings["WizomCon"].ConnectionString);
    String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
           
            if (Session["usr"].ToString() != null)
            {
                Image1.ImageUrl = "~/ImgHandler.ashx?id=" + Session["usr"].ToString();
                Label6.Text = Convert.ToString(Session["usr"]);
               
            }
        }
        catch
        {
              Response.Redirect("~/Wizom_Login.aspx");
        }
    }

    private byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage)
    {
        byte[] Ret;
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageToConvert.Save(ms, formatOfImage);
                Ret = ms.ToArray();
            }
        }
        catch (Exception) { throw; }
        return Ret;
    }
   
    //protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    //{
        
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (!flImage1.HasFile)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Must Choose An .jpeg,.png Image');", true);
            }
            else if (Session["usr"].ToString() != null)
            {
                FileUpload fu = flImage1;
                String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();

                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        try
                        {
                            if (IsPostBack)
                            {

                                SqlConnection con = new SqlConnection(constr);
                                System.Drawing.Image imag = System.Drawing.Image.FromStream(flImage1.PostedFile.InputStream);
                                if (con.State == ConnectionState.Closed)
                                {
                                    con.Open();
                                }
                                string un = Session["usr"].ToString();
                                SqlCommand cmd = new SqlCommand("SPUploadImage", con);

                                cmd.CommandType = CommandType.StoredProcedure;

                                cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = un;
                                cmd.Parameters.Add("@image", SqlDbType.Image, 0).Value = ConvertImageToByteArray(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
                                cmd.ExecuteNonQuery();
                                con.Close();

                            }
                            Response.Redirect("WizomProfile.aspx");
                        }

                        catch
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('There was an upload error, Make Sure To Upload a .jpeg,.png or .gif file');", true);

                        }
                    }

                }
            }
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('There was an upload error.<br>Make Sure To Upload a .jpeg,.png or .gif file Try Again);", true);

        }
    }
}
Posted
Updated 1-Jul-13 20:28pm
v2
Comments
Thanks7872 2-Jul-13 2:58am
   
Remove unnecessary code.No one would go through this type of lengthy codes. Response will be very poor.

1 solution

   
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900