开发者

how to open MS Office word in asp.net?

in my application, when user click on particular link button, MS word has to open开发者_开发百科 how can i write the code for this. Thank you


There is no way to guarantee that a particular application will be opened when a user clicks a link / button on a web page. The application that is opened is determined by the user's browser and operating system settings.

As a developer you can specify the MIME type of the file that you are returning. By doing so you are telling the user's browser what file type is contained in the response. W3Schools provides a pretty good MIME type by content type list and FILExt also provides the MIME type for the files it lists.

Assuming you specify the appropriate MIME content-type you can be sure the user's browser and operating system will open the file in the "appropriate" application according to their settings. Since you want to open a Word Document file the appropriate MIME content-type will be one of the following:

 Extension Type/sub-type
 docx      application/vnd.openxmlformats-officedocument.wordprocessingml.document
 doc       application/msword
 

How and where you specify the content-type largely depends on the ASP.NET application type you are working with. If you are writing a ASP.NET Webforms application you'd change the MIME type of the Response object in the Page_Load method. In an ASP.NET MVC application you'd do so in a controller action. In either case the specific line of code is the same.

Response.ContentType = "application/msword";


When you click on a link, redirect the browser to the file mydoc.docx - the browser will open that in Word as long as they have Word installed. You will also need to maek sure your IIS server has the MIME type setup, see Downloading Docx from IE - Setting MIME Types in IIS

If you are generating the Word document on the fly, you will need to set the MIME type so that the browser knows that the response is a word document. I suggest the docx format for generating content.


If it is a Windows Application, this should work...

System.Diagnostics.Process.Start("FileName.doc");

For a web application, just redirect to the filename on the hosted server, or use one of the ways as described in the post below...

http://www.dotnetscraps.com/dotnetscraps/post/4-ways-to-send-a-PDF-file-to-the-IE-Client-in-ASPNET-20.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜