SIGN UP MEMBER LOGIN:    
ARTICLE

How to open a Word document from an ASP.NET application

Posted by Amit M Articles | ASP.NET Programming March 30, 2011
This article demonstrates how to open a Word document in an ASP.NET application.
Reader Level:


Introduction

Sometimes we need to open a Microsoft Word document in an ASP.NET application.

This article demonstrates how to open a Word document in an ASP.NET application.

What do we need to open document?

Response.ContentType = "application/vnd.msword.document.12";

The ContentType property specifies the HTTP content type for the response.

If no ContentType is specified, the default is text/HTML.

We could say ContentType is a DocType, so we could set

ContentType by using the code shown below:

        private string GetDocumentMimeType(string fileName)
        {
            //string mimeType = "application/unknown";
            string mimeType = "application/vnd.msword.document.12";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();

            return "//" + mimeType;
        }


In an ASP.NET application we need to add the header: Content-Disposition,Content-Length

Content-Disposition: attachment

Body parts designated as containing attachments require user action to be displayed and are normally split out of the message. They are typically stored as files for subsequent access. The optional parameters for this header are: file name, creation-date, modification-date, read-date, and size.

What does the AddHeader method do?

The AddHeader method adds a new HTML header and value to the response sent to the client.

Login to add your contents and source code to this article
share this article :
post comment
 

In article, i have given the whole code to download the word doc. what do you mean by fill? In webapplication you can't save directly into ur save folder(server folder).b'coz client may open it from any where. you have to give upload option to the user to save the document where you want to save at server folder.

Posted by Amit M Apr 08, 2011

hi I have one query i want to download the word.doc and fill the doc and submit the doc to save the folder how its happened guide to me

Posted by Ramesh sharma Apr 08, 2011

Nice one

Posted by hamida khanam Mar 31, 2011

Nice one

Posted by hamida khanam Mar 31, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor