File Upload in asp.net using c#

Introduction:
  How to create File Upload and .jpg format only accepted.


Solutions:
 The following coding is solved for above problem.


step 1:  first create File Upload control and one button from tool box in asp.net 2008.

step 2: then copy and paste inside of button onclick() event.


protected void btn_Upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
           
            string getFileName = FileUpload1.FileName;
            string[] getSplitName = getFileName.Split('.');
            if (getSplitName[1] == "JPEG" || getSplitName[1] == "jpg")
            {
                string getPath = HttpContext.Current.Server.MapPath("Upload/" + getFileName);
                FileUpload1.SaveAs(getPath);
                lbl_filename.Text = FileUpload1.PostedFile.FileName;
            }
            else
            {
                lbl_filename.Text = "Jpeg format is only Accepted";
            }
        }
        else
        {
            lbl_filename.Text = "No File Selected";
        }
       
       
    }

Comments