3

how can i open a .docx ,.doc,.xls or .ppt file in asp.net mvc application considering my file is stored in the database. I can open a css file in notepad by writing the following line

return File(("~/Content/Site.css"), ("text/css"));
  • posible duplicate : http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-mvc – Sreekumar P Dec 31 '12 at 08:43
  • Where do you want to open the file, on the client? Will he have Microsoft office, or appropriate viewers installed? – SWeko Dec 31 '12 at 08:45
  • @SWeko : yes. He will have MS Office. I want that .doc or .docx files which are stored in database should get opened in MS word. Same goes for .ppt in powerpoint and .xls in Excel –  Dec 31 '12 at 09:01

2 Answers2

1

The File utility method (for FileResult) has an overload that takes a stream or a byte array (your file in a db can be provided as a stream or a byte array.

  var bytes = db.GetFile(); //i'm assuming this method will return a byte array
   return File(bytes, 
     "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

To download the file using a particular file name, you can use another overload.

return File(bytes, 
       "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "filename.docx");
gideon
  • 19,329
  • 11
  • 72
  • 113
scartag
  • 17,548
  • 3
  • 48
  • 52
  • can you please elaborate. I am new to MVC. I tried using the utility of File method but how to open a file with a certain extension(.docx,.doc,.ppt etc.) –  Dec 31 '12 at 08:42
  • It does work dude. Just 1 question. What will I have to do to open excel and power point file.? Do i have to make changes in this word " wordprocessingml", remove word and put excel.? –  Dec 31 '12 at 09:07
0

It seems to me that you do not need to "open the file", but you need the client to be able to download the file from your application, and than do whatever clients do with downloaded files. In most cases, the browser will display a save/open dialog, but that is job for the end-user client (maybe he has some plug-ins installed?), and not for the site.

However, to tell the site to offer an office, there is a list of content types here, and the ones that interest you are:

docx -> "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
pptx -> "application/vnd.openxmlformats-officedocument.presentationml.presentation"
xlsx -> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

so you'll need to do something like (pseudo-code)

Dictionary<string, string> contentTypes = new Dictionary<string, string>();
contentTypes.Add("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
contentTypes.Add("pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation");
contentTypes.Add("xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");;

string fileType = ...;

var result = new File(fileContents, contentTypes[fileType],"fileName."+fileType);
return result;
SWeko
  • 30,434
  • 10
  • 71
  • 106