File Upload

Today we will discuss how to  in asp.net webpage. It’s a very simple script to disable right click event. You can disable the right click event in whole website by adding this script in master page.

On upload button click:
protected void btn_upload_Click(object sender, EventArgs e)
    {
        if (fp_id_img.HasFile)
        {
            if (fp_id_img.PostedFile.ContentType == "image/jpeg" || fp_id_img.PostedFile.ContentType == "image/jpg" || fp_id_img.PostedFile.ContentType == "image/gif" || fp_id_img.PostedFile.ContentType == "image/png")
            {
                string img_id = Guid.NewGuid().ToString().Substring(0, 10);
                filepath = "~/Images/Id_proof_img/" + img_id + Class1.Getextention(fp_id_img.FileName);
                filename = Path.GetFileName(fp_id_img.FileName);
               fp_id_img.SaveAs(Server.MapPath(filepath));
                Image2.Visible = true;
                Image2.ImageUrl = filepath;

                string msg1 = string.Empty;
                msg1 = "Picture uploaded successfully";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg1 + "');", true);
                return;
               
            }
            else
            {
                string msg1 = string.Empty;
                msg1 = "Only jpeg,jpg,gif,png can be Uploaded!";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg1 + "');", true);
                return;
            }
        }

    }


In class file following extension must be added.
#region extension
    public static string Getextention(string Filename)
    {
        string _extention;
        int index;
        index = Filename.IndexOf('.');
        _extention = Filename.Substring(index);
        return _extention;

    }
    #endregion



Get Image from database

if (dr.Tables[0].Rows.Count > 0)
{

Image_edit.ImageUrl = dr.Tables[0].Rows[0]["id_img"].ToString();
}

//"id_img" is name of column name in which image path is save d in database.




Summary
In this article I try to expose File Upload in asp.net. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

No comments:

Post a Comment