首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在asp.net-mvc中打开文件

在asp.net-mvc中打开文件
EN

Stack Overflow用户
提问于 2012-12-31 16:36:35
回答 2查看 827关注 0票数 3

考虑到我的文件存储在数据库中,我如何在asp.net mvc应用程序中打开.docx、.doc、.xls或.ppt文件。我可以通过编写以下代码行在记事本中打开css文件

代码语言:javascript
复制
return File(("~/Content/Site.css"), ("text/css"));
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-31 16:50:35

在我看来,您不需要“打开文件”,但您需要客户端能够从您的应用程序下载文件,然后执行客户端对下载的文件所做的任何操作。在大多数情况下,浏览器会显示一个保存/打开对话框,但这是终端用户客户端的工作(也许他安装了一些插件?),而不是站点。

然而,为了告诉网站提供办公室,有一个list of content types here,你感兴趣的是:

代码语言:javascript
复制
docx -> "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
pptx -> "application/vnd.openxmlformats-officedocument.presentationml.presentation"
xlsx -> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

所以你需要做一些类似(伪代码)的事情

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2012-12-31 16:38:24

File实用程序方法(用于FileResult)有一个接受流或字节数组的重载( db中的文件可以作为流或字节数组提供)。

代码语言:javascript
复制
  var bytes = db.GetFile(); //i'm assuming this method will return a byte array
   return File(bytes, 
     "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

若要使用特定文件名下载文件,可以使用另一个重载。

代码语言:javascript
复制
return File(bytes, 
       "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "filename.docx");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14099394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档