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"));
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"));
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");
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;