2
Answers

How to read word document in C# while uploading the file?

sudharsan s

sudharsan s

11y
2.5k
1
My code:

In my project i am uploading various files. while i am uploading i want to read the text of the word document and save in to db.

here i am done oly for text files ....So help me how to write the code to save word text in db...while i am uploading


code:

 protected void cmdAdd_Click(object sender, EventArgs e)
  {
  //if (getoid() != "")
  //{
 
  SqlConnection con = new SqlConnection(constring);

  con.Open();

  string filename = txtFileUpload.FileName;

  //string size=txtFileUpload.FileBytes);//.ToString();

  int size1= txtFileUpload.PostedFile.ContentLength;

  int size = size1 / 1024;

  txtFileUpload.SaveAs(Server.MapPath("uploads/" + filename));

  string fname = Path.GetFileName(filename);

  //string myDateTime=demo1.Text;

  //DateTime myDateTime = DateTime.Parse(myDate);

  string query = string.Empty;

  //string res = DropDownList1.SelectedItem as string;

  string extn = Path.GetExtension(filename);

  string key = keyword1.Text;

  if (extn.Equals(".txt"))

  {

  TextReader tr = new StreamReader(Server.MapPath("uploads/" + filename));

  query = "declare @ret varchar(100) INSERT INTO Fileupload([ref_id],[category],[subcategory],[uid],[oid],[up_name],[demo1],

[keyword],[fdata],[aflag],sizebyte) VALUES('" + ViewState["id"].ToString() + "','" + dropcategory.SelectedItem + "','" +

DropDownList1.SelectedItem + "','" + Session["user_id"].ToString() + "','" + txtOwnerId.SelectedItem + "','" + fname + "',getdate(),'" + key +

"','" + tr.ReadToEnd().TrimEnd() + "',1,'" + size + "') select @ret=ref_id from Fileupload select @ret ";

  // create reader & open file

  }

  else

  {

  query = @" declare @ret varchar(100) INSERT INTO Fileupload

  ([ref_id]

  ,[category]
  ,[subcategory]
  ,[uid]
  ,[oid]
  ,[up_name]
  ,[keyword]
  ,[demo1]
  ,[aflag]
  ,[sizebyte])
  VALUES
  ('" + ViewState["id"].ToString() + "','" + dropcategory.SelectedItem + "','" + DropDownList1.SelectedItem + "','" +

Session["user_id"].ToString() + "','" + txtOwnerId.SelectedItem + "','" + fname + "','" + key + "',getdate(),1,'" + size + "') select @ret=ref_id

from Fileupload select @ret ";
  }
  SqlCommand cmd = new SqlCommand(query, con);

  string getval= cmd.ExecuteScalar().ToString();

  ref_id();

  //gvCategory.DataBind();
  ////load_grid();

  Page.RegisterStartupScript("onclick", "<script>alert('uploads successfully.. ref_id=" + getval + "')</script>");

  load_repeter(); 
  //}
 
  }


Answers (2)