I have a View with Browse and Submit button.When the User Clicks on browse a .doc or .docx file can be browsed and when the Submit button is clicked the selected file’s Text should get populated in the TextBox on the same View.
Below is my code to read and Display the Text in TextBox.
string filePath =null,docText=null;
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase Infile = Request.Files[inputTagName];
if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == ".doc"))
{
filePath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
Path.GetFileName(Infile.FileName));
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
Infile.SaveAs(filePath);
}
if (filePath != null)
{
docText = System.IO.File.ReadAllText(filePath);
}
ViewBag.displayTextInTextBox= docText;
}
return View();
Below is my View Code
<input type="text" id="test1" name="test" value="@ViewBag.displayTextInTextBox">
Its showing Special characters(like this ��ࡱ� ) rather than the Text in the .doc/.docx document.
Am i reading the file incorrectly or what’s the issue in my code.
Instead of using Word Automation, which would require installing Word on your server (which might not be a good idea) I would look at extracting information from the word document using the OpenXML SDK:
http://www.microsoft.com/download/en/details.aspx?id=5124
Note that this would not work for .doc-files, just docx.