File Upload

S

 Source Code



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>



            <b>UPLOAD A FILE </b>

            <br />

            <asp:Image ID="imgProfile" runat="server" BorderStyle="Double"

                BorderWidth="5px" Height="300px" Width="300px" />

            &nbsp;<asp:FileUpload ID="PictureUpload" runat="server" />

            <asp:Button ID="btnUplaod" runat="server" OnClick="btnUplaod_Click"

                Text="Upload" />
              <asp:Button ID="btnShow" runat="server" OnClick="btnUplaod_Click"

                Text="Show" />

              insert a image control with id  " imgProfile " here....

        </div>

    </form>

</body>

</html>


C# Code



Add a folder (" UserProfilePictuers ")  in asp project on solution explorer .....
 

protected void  btnUplaod_Click (object sender, EventArgs e)

        {

            //id of fileupload control  : "PictureUpload"

            string DBpath;

            string Imgpath = Server.MapPath("~/UserProfilePictuers/");//set path to folder(in UI)

            string extention = System.IO.Path.GetExtension(PictureUpload.FileName);//get extention of item

            if (extention == ".jpg" || extention == ".png")// check item ext

            {

                PictureUpload.SaveAs(Imgpath + PictureUpload.FileName);//save image to folder(physical path)

                DBpath = "/UserProfilePictuers/" + PictureUpload.FileName;//this path saved in Database using ExicuteNonQry Function

                imgProfile.ImageUrl = DBpath;//show in image control

            }

           

           

        }






     Read file from database and show it

1.  Fetch image of a user depend on UserId Entered in  TextBox ("txtId")



         protected void   btnShow_Click (object sender, EventArgs e)

            {



         SqlConnection con = new SqlConnection("Data Source=10.18.184.133;
         Initial  Catalog=ABC_Demo;User ID=niituser;Password=niituser");


            string str= "Select  file_name from table where empId='"+txtId.text+"'";extention of
            SqlCommand  cmd = new SqlCommand(str, con);
            SqlDataReader   reader = cmd.ExecuteReader();
             while (rder.Read())
                {
               imgProfile.ImageUrl=Convert.ToString( rder["filename"]);
                }

            }

           

           

        }
// filename is a column in table .....

1. after we upload a file (btnUplaod_Click )-------

 PictureUpload.SaveAs(Imgpath + PictureUpload.FileName); //save image to  folder(physical    path)
 DBpath = "/UserProfilePictuers/" + PictureUpload.FileName;//this path saved in  table column ("filename" )using ExicuteNonQry Function 
 so   this column contain  value..."/UserProfilePictuers/imageName.jpg";

2. When i click on show button ("btnShow_Click ") fetch above value, and set as image url of image control
that is  ==> imgProfile.ImageUrl=/UserProfilePictuers/imageName.jpg;

that is "UserProfilePictuers" is a folder and "imageName.jpg"  is a file in this folder....


No comments:

Post a Comment