Mar 5, 2010

Image uploading and store in a folder in asp.net using file upload control

<tr>
                            <td align="right">
                                Image</td>
                            <td align="left">
                                <asp:FileUpload ID="FileUpload1" runat="server" Height="22px"
                                     Width="217px" />
                                <asp:Button ID="btnupload" runat="server" onclick="btnupload_Click"
                                    Text="Upload" CausesValidation="False" />
                            </td>
                        </tr>


protected void btnupload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {

            //FileUpload1.SaveAs(@"C:\temp\" + FileUpload1.FileName);
            //  Server.MapPath("./images/Upload")
            //lblrespns.Text = "File Uploaded: " + FileUpload1.FileName;


            //string destDir  = Server.MapPath("./screen/imgs/ths");
            string fName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            fName = "Temp" + ".jpg";
            Session["filename"] = fName;
            string ImagePath = Server.MapPath("~/screen/imgs/ths/") + fName;

            if (File.Exists(ImagePath) == true)
            {
                File.Delete(ImagePath);

            }

            FileUpload1.SaveAs(Server.MapPath("~/screen/imgs/ths/") + fName);






            //string destPath = Path.Combine(destDir, fName);

            lblrespns.Text = "Upload succeeded.";

            Image1.ImageUrl = "~/screen/imgs/ths/" + fName;
        }
        else
        {
            lblrespns.Text = "No File Uploaded.";
        }
    }








No comments:

Post a Comment