Ref
Refer this code. It will read the content text.
HTML
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<br />
<hr />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false" EmptyDataText="No records found">
<Columns>
<asp:BoundField DataField="DocumentId" HeaderText="DocumentId" ItemStyle-Width="30" />
<asp:BoundField DataField="DocumentName" HeaderText="DocumentName" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfHtmlContent" runat="server" Value='<%# Eval("Content") %>' />
<asp:LinkButton Text="View" OnClick="View" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<div id="divHtmlContent" runat="server" visible="false">
</div>
</div>
<br />
</form>
Add this property in Page directive
ValidateRequest="false"
And this line in the Webconfig file under System.Web
<httpRuntime requestValidationMode = "2.0" />
Namespaces
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateGridView();
}
}
private void PopulateGridView()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Documents", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
protected void View(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
divHtmlContent.Visible = true;
divHtmlContent.InnerHtml = (row.FindControl("hfHtmlContent") as HiddenField).Value;
}
protected void Upload(object sender, EventArgs e)
{
if (this.FileUpload1.HasFile)
{
object missingType = Type.Missing;
object readOnly = true;
object isVisible = false;
object documentFormat = 8;
string randomName = DateTime.Now.Ticks.ToString();
object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
FileUpload1.SaveAs(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
object fileName = FileUpload1.PostedFile.FileName;
ApplicationClass applicationclass = new ApplicationClass();
applicationclass.Documents.Open(ref fileName,
ref readOnly,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref isVisible,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType);
applicationclass.Visible = false;
Document document = applicationclass.ActiveDocument;
document.SaveAs(ref htmlFilePath, ref documentFormat, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType);
document.Close(ref missingType, ref missingType, ref missingType);
File.Delete(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
string generatedHTML = string.Empty;
using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
{
StreamReader sReader = new StreamReader(fs);
generatedHTML += sReader.ReadToEnd();
fs.Close();
}
string htmlContent = generatedHTML.Substring(generatedHTML.IndexOf("<div"));
htmlContent = htmlContent.Substring(0, htmlContent.IndexOf("</div>") + 6);
htmlContent = htmlContent.Replace("class=WordSection1", "");
htmlContent = htmlContent.Replace("class=MsoNormal", "");
//Save the file content
this.SaveFile(Path.GetFileName(FileUpload1.PostedFile.FileName), htmlContent);
File.Delete(htmlFilePath.ToString());
foreach (string file in Directory.GetFiles(directoryPath))
{
File.Delete(file);
}
Directory.Delete(directoryPath);
}
}
private void SaveFile(string documentName, string content)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sqlStatment = "INSERT INTO Documents VALUES(@DocumentName, @Content)";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@DocumentName", documentName);
cmd.Parameters.AddWithValue("@Content", content);
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
SQL
CREATE TABLE [dbo].[Documents](
[DocumentId] [int] IDENTITY(1,1) NOT NULL,
[DocumentName] [varchar](50) NOT NULL,
[Content] [nvarchar](max) NOT NULL
) ON [PRIMARY]
GO
Screenshot
