Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to display word document file in the browser .

I also write a code here.
but it is not open in browser but first open pop up window for download or open the file.

But i want to read file in the browser not a downloaded file is open
Posted
Updated 7-Aug-17 9:30am
v2

Just embed Google Doc Viewer through an iframe and specify the DOC file you want to display. This is the code you should add:

HTML
<iframe src="http://docs.google.com/gview?url=http://www.YOUR_DOMAIN.com/YOUR_WORD.DOC&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>
 
Share this answer
 
ah, sorry.. I misunderstood.
In that case I think you need to convert the doc file to html and then load html content directly on browser. Here is sample code to convert doc to html and then load the html in browser:
private void OpenMSWordFileByBrowser()
    {
        string htmlFilePath = "E:\\test.html";
        Convert("E:\\test.docx",htmlFilePath, WdSaveFormat.wdFormatHTML);
        //
        Response.ClearContent();
        Response.ClearHeaders();
        Response.WriteFile(htmlFilePath);
        Response.Flush();
        Response.Close();
    }
    private static void Convert(string docFilePath,string htmlFilePath, WdSaveFormat format)
    {

        DirectoryInfo dirInfo = new DirectoryInfo(docFilePath);
        FileInfo wordFile = new FileInfo(docFilePath);
        //
        object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
        try
        {
            word.Visible = false;
            word.ScreenUpdating = false;
            //
            Object filename = (Object)wordFile.FullName;
            Document doc = word.Documents.Open(ref filename, ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                               ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            try
            {
                doc.Activate();
                object outputFileName = htmlFilePath;
                object fileFormat = format;
                doc.SaveAs(ref outputFileName,
                           ref fileFormat, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            }
            finally
            {
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                doc = null;
            }
        }
        finally
        {
            ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
            word = null;
        }
    }


Instead of physical file location you can use relative path if the doc file exists in your application folder and use server.mappath.
 
Share this answer
 
Comments
Nelek 4-May-14 16:20pm    
Please, next time, use the "improve solution" widget and edit you message to correct / add / replace what you want. Don't post a second message.
altaf008bd 6-May-14 0:57am    
Thanks for the suggestion, I was not aware of that. I will follow it for all future posts.
imsureshthapa 11-Aug-16 0:13am    
Yes, this worked for localhost, But I got permission error 'Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied.' while hosting on IIS. I gave all permission to IUSER but still not worked. Any Solution?
Hi, Here is the code to open a word document file:
Context.Response.Clear();
        FileInfo file = new FileInfo("E:\\test.docx");
        Context.Response.ContentType = "Application/msword";
        Context.Response.AppendHeader("Content-Disposition", "inline; filename=" + file.Name);
        Context.Response.AppendHeader("Content-Length", file.Length.ToString());
        Context.Response.WriteFile(file.FullName);
        Context.Response.End();
 
Share this answer
 
Comments
M Kashif Saleem 1-May-14 2:13am    
Dear I don't want to open the popup of open the file or download the file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900