Friday, February 27, 2015

upload text file

  SqlConnection con = new SqlConnection("Data Source=ADMIN-0A8466DDF\\SQLEXPRESS;Initial Catalog=nani;Integrated Security=True");
    string path;
    protected void txtbtn_Click(object sender, EventArgs e)
    {
        String ext = System.IO.Path.GetExtension(FileUpload1.FileName);
        //string filetext = System.IO.Path.GetExtension(path);
        if (FileUpload1.HasFile)
        {
            if (ext == ".txt")
        //  if(FileUpload1.PostedFile.FileName.ToLower().Substring(FileUpload1.PostedFile.FileName.Length)=="txt/docx")
       
          // FileUpload1.PostedFile.FileName.ToLower().Substring(FileUpload1.PostedFile.FileName.Length - 4) == "docx"
            {
                path = Server.MapPath("~/Textfile/" + FileUpload1.FileName);
                FileUpload1.SaveAs(path);
                SqlCommand cmd = new SqlCommand("insert into uploading values('" + txtname.Text + "','" + path + "')", con);
                con.Open();
                int i = cmd.ExecuteNonQuery();
                Response.Write(i + "record inserted");
            }
            else
            {
                Response.Write("upload text file");
            }
        }
        else
        {
            Response.Write("upload failed");
        }
        con.Close();
    }

No comments:

Post a Comment