0

I have a Preview button on my page. I want to display a .doc file in the browser when the user clicks on that button. I have written the code below, but it directly downloads the file instead of displaying it in the browser.

public FileResult ViewFile() {
        Response.AddHeader("content-disposition", "inline;filename=;");
        return File("~/Templates/chronoExp.doc", "application/msword");
} 
ADyson
  • 57,178
  • 14
  • 51
  • 63
Zain
  • 37
  • 1
  • 6
  • browsers can't read `doc` files by default you should use some sort of js library to make it work – teo van kot Dec 13 '16 at 14:04
  • This _might_ work in IE if the browser / computer is set up like that. But essentially it's still a download, just that there's an in-browser display mode that can be used if set up and installed. For other browsers, you'll need some other sort of plugin, or host the doc in a cloud service like Office365 or something instead. I'm surprised you didn't already find an answer to this on google TBH. – ADyson Dec 13 '16 at 14:08

1 Answers1

0

You will need some sort of control or library that's capable of showing the file type within the browser. By default, browsers are not able to display an office document file (I believe PDF is the only document style that gets relatively decent support for embedded viewing across all browsers).

In order to do this, look for some 3rd party tools or controls that would allow for this to work. Such tools will involve passing the document to them and likely some server-side transformation and render to the control's browser display, so it's likely to be read-only.

This answer provides a decent example of using Google's document viewer within an iframe: How do I render a Word document (.doc, .docx) in the browser using JavaScript?

Community
  • 1
  • 1
jleach
  • 7,410
  • 3
  • 33
  • 60