Wednesday, June 22, 2016

File Upload in ASP.Net

 protected void Button1_Click(object sender, EventArgs e)
    {
        //code to Insert into database
        string nm = TextBox1.Text.Trim();
        string usernm = TextBox2.Text.Trim();
        string pass = TextBox3.Text.Trim();
        string phone = TextBox4.Text.Trim();
        if (FileUpload1.HasFile) {
            string name=FileUpload1.PostedFile.FileName;
            string ext=System.IO.Path.GetExtension(name);

            if(!(ext==".jpg" || ext==".png" || ext == ".bmp")){
            Label8.Text="Invalid File Type!! Only Images(jpg,bmp or png) allowed!";
                return;
            }
            else if(FileUpload1.PostedFile.ContentLength > 100000){
            Label8.Text="Image too large!! Only Upto 100Kb Allowed!!";
                return;
            }
            string filename = DateTime.Now.ToFileTime() + usernm+ ext;
            string ins = "insert into register(username,name,password,phone,image) values('"+usernm+"','"+name+"','"+pass+"','"+phone+"','"+filename+"')";
            FileUpload1.SaveAs(Server.MapPath("Images") + "/"+filename);
            int i = dbase.insert(ins);
            if (i == 1) {
                Label8.Text = "Registered successfully!!";
             }
            else
                Label8.Text = "Error while saving!!!!";
           
        }
    }

No comments:

Post a Comment