Ejemplo n.º 1
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            HttpPostedFile postedFile = PicUpload.PostedFile;

            string imgName = PicUpload.FileName;
            string imgPath = "Images/" + imgName;

            int imgSize = PicUpload.PostedFile.ContentLength;

            string fileExtension = Path.GetExtension(imgName);

            if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".gif" ||
                fileExtension.ToLower() == ".png" || fileExtension.ToLower() == ".bmp")
            {
                PicUpload.SaveAs(Server.MapPath("../../../" + imgPath));
                Session["Sketch"] = imgPath;
                p = Session["Sketch"].ToString();
            }
            else
            {
                lblMsg.Visible   = true;
                lblMsg.ForeColor = System.Drawing.Color.Red;
                lblMsg.Text      = "Only images (.jpg, .png, .gif and .bmp) can be uploaded";
            }
        }
    public String ImgUpload()
    {
        string filename     = "";
        String path         = "";
        String completepath = "";

        try
        {
            if (PicUpload.HasFile && (PicUpload.PostedFile.ContentType == "image/jpeg" || PicUpload.PostedFile.ContentType == "image/png" || PicUpload.PostedFile.ContentType == "image/gif"))
            {
                //pic is present and is of correct format

                filename = PicUpload.FileName;
                path     = Server.MapPath("~/images/userprofilepic/");
                String path_without_extension;

                if (PicUpload.PostedFile.ContentType == "image/jpeg")
                {
                    filename = "pp_" + Session["user_id"].ToString() + ".jpg";
                }
                else if (PicUpload.PostedFile.ContentType == "image/png")
                {
                    filename = "pp_" + Session["user_id"].ToString() + ".png";
                }
                else if (PicUpload.PostedFile.ContentType == "image/gif")
                {
                    filename = "pp_" + Session["user_id"].ToString() + ".gif";
                }

                completepath = path + filename;

                path_without_extension = path + "pp_" + Session["user_id"].ToString();

                if (File.Exists(path_without_extension + ".jpg") || File.Exists(path_without_extension + ".png") || File.Exists(path_without_extension + ".gif"))
                {
                    //deleteing the previous existing file , jpg or png or gif

                    if (File.Exists(path_without_extension + ".jpg"))
                    {
                        File.Delete(path_without_extension + ".jpg");
                    }
                    if (File.Exists(path_without_extension + ".png"))
                    {
                        File.Delete(path_without_extension + ".png");
                    }
                    if (File.Exists(path_without_extension + ".gif"))
                    {
                        File.Delete(path_without_extension + ".gif");
                    }
                }


                PicUpload.SaveAs(completepath);

                return(filename);
            }
            else
            {
                return(null);
            }
        }

        catch
        {
            return(null);
        }
    }