Sam M
Sam M

Reputation: 1087

How to read a .doc and .docx file in Asp.Net MVC3 and display in TextBox

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.

Upvotes: 3

Views: 4592

Answers (3)

Sam M
Sam M

Reputation: 1087

Thank You friends so much for Your Help. Below is what i have done and it solved the Issue,

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new 

    Microsoft.Office.Interop.Word.ApplicationClass();
                    string filePath1 = filePath;
                    object file = filePath1;
                    object nullobj = System.Reflection.Missing.Value;

   Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file,
                 ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                 ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                                                                 ref nullobj);
                 Microsoft.Office.Interop.Word.Document doc1 = wordApp.ActiveDocument;
                 string m_Content = doc1.Content.Text;
                 ViewBag.test = m_Content;
                 doc.Close(ref nullobj, ref nullobj, ref nullobj);

Im using the COM object of MSWord. Hope this is the best Possible way to do it.

Upvotes: -2

granaker
granaker

Reputation: 1328

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.

Upvotes: 4

user1006544
user1006544

Reputation: 1524

Well Sam you can see my Question here, As I have also asked earlier ,If you can find it useful . Actually for this type of problem you need to explore yourself the classes and use it accoring to you. This ans will give you the basics rest you have to do.

Upvotes: 2

Related Questions