考虑到我的文件存储在数据库中,我如何在asp.net mvc应用程序中打开.docx、.doc、.xls或.ppt文件。我可以通过编写以下代码行在记事本中打开css文件
return File(("~/Content/Site.css"), ("text/css"));发布于 2012-12-31 16:50:35
在我看来,您不需要“打开文件”,但您需要客户端能够从您的应用程序下载文件,然后执行客户端对下载的文件所做的任何操作。在大多数情况下,浏览器会显示一个保存/打开对话框,但这是终端用户客户端的工作(也许他安装了一些插件?),而不是站点。
然而,为了告诉网站提供办公室,有一个list of content types here,你感兴趣的是:
docx -> "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
pptx -> "application/vnd.openxmlformats-officedocument.presentationml.presentation"
xlsx -> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"所以你需要做一些类似(伪代码)的事情
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;发布于 2012-12-31 16:38:24
File实用程序方法(用于FileResult)有一个接受流或字节数组的重载( db中的文件可以作为流或字节数组提供)。
var bytes = db.GetFile(); //i'm assuming this method will return a byte array
return File(bytes,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document");若要使用特定文件名下载文件,可以使用另一个重载。
return File(bytes,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"filename.docx");https://stackoverflow.com/questions/14099394
复制相似问题