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", "");
load_repeter();
//}
}
Loading
Mark ConorsPosted Mar 11, 2013, 3:36 AM
you can use Elerium .NET Word Reader to read text from .doc/.docx files. It is very fast and powerful component, but it is not free. Or you can use Microsoft Interop as it's done in these articles:
http://www.mindstick.com/Articles/5cd1b721-9b94-4ea0-bd6e-2bb157401069/
http://www.codeproject.com/Articles/3959/Microsoft-Word-Documents-from-ASP-NET
George ericPosted Apr 26, 2012, 4:22 AM
if word is not structured, then you can convert doc to txt.
With Spire.doc you can convert word to text:
//Create word document
Document document = new Document();
document.LoadFromFile(@"D:\Work\Stephen\2011.12.05\Sample.doc");
//Save doc file.
document.SaveToFile("Sample.txt", FileFormat.Txt);
//Launching the MS Word file.
WordDocViewer("Sample.txt");
Note it is commericial in davanced. Good luck