欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > >内容正文

asp.net

asp.net mvc:通过 fileresult 向 浏览器 发送文件 -尊龙游戏旗舰厅官网

发布时间:2025/1/21 15 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 asp.net mvc:通过 fileresult 向 浏览器 发送文件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在 controller 中我们可以使用 fileresult 向客户端发送文件。

fileresult 是一个抽象类,继承自 actionresult。在 system.web.mvc.dll 中,它有如上三个子类,分别以不同的方式向客户端发送文件。

在实际使用中我们通常不需要直接实例化一个 fileresult 的子类,因为 controller 类已经提供了六个 file 方法来简化我们的操作:

protected internal filepathresult file(string filename, string contenttype); protected internal virtual filepathresult file(string filename, string contenttype, string filedownloadname); protected internal filecontentresult file(byte[] filecontents, string contenttype); protected internal virtual filecontentresult file(byte[] filecontents, string contenttype, string filedownloadname); protected internal filestreamresult file(stream filestream, string contenttype); protected internal virtual filestreamresult file(stream filestream, string contenttype, string filedownloadname);

filepathresult 直接将磁盘上的文件发送至浏览器:

1. 最简单的方式

public actionresult filepathdownload1() {var path = server.mappath("~/files/鹤冲天.zip");return file(path, "application/x-zip-compressed"); }

第一个参数指定文件路径,第二个参数指定文件的 mime 类型。

用户点击浏览器上的下载链接后,会调出下载窗口:

大家应该注意到,文件名称会变成 download1.zip,默认成了 action 的名字。我们使用 file 方法的第二个重载来解决文件名的问题:

2. 指定 filedownloadname

public actionresult filepathdownload2() {var path = server.mappath("~/files/鹤冲天.zip"); return file("g:\\鹤冲天.zip", "application/x-zip-compressed", "crane.zip"); }public actionresult filepathdownload3() { var path = server.mappath("~/files/鹤冲天.zip"); var name = path.getfilename(path);return file(path, "application/x-zip-compressed", name); }

我们可以通过给 filedownloadname 参数传值来指定文件名,filedownloadname 不必和磁盘上的文件名一样。下载提示窗口分别如下:

filepathdownload2 没问题,filepathdownload3 还是默认为了 action 的名字。原因是 filedownloadname 将作为 url 的一部分,只能包含 ascii 码。我们把 filepathdownload3 改进一下:

3. 对 filedownloadname 进行 url 编码

public actionresult filepathdownload4() {var path = server.mappath("~/files/鹤冲天.zip");var name = path.getfilename(path);return file(path, "application/x-zip-compressed", url.encode(name)); }

再试下,下载窗口如下:

好了,没问题了。上面代码中 url.encode(…),也可使用 httputility.urlencode(…),前者在内部调用后者。

我们再来看 filecontentresult.

filecontentresult 可以直接将 byte[] 以文件形式发送至浏览器(而不用创建临时文件)。参考代码如下:

public actionresult filecontentdownload1() {byte[] data = encoding.utf8.getbytes("欢迎访问 鹤冲天 的博客 http://www.cnblogs.com/ldp615/");return file(data, "text/plain", "welcome.txt"); }

点击后下载链接后,弹出提示窗口如下:

想给 filestreamresult 找一个恰当的例子是不太容易的,毕竟 http response 中已经包含了一个输出流,如果要动态生成文件的话,可以直接向这个输出流中写入数据,效率还高。当然,我们不会在 controller 中直接向 response 的 outputstream 写入数据,这样做是不符合mvc的,我们应该把这个操作封装成一个 actionresult。

不过仔细想想,用途还是有的,比如服务器上有个压缩(或加密)文件,需要解压(或解密)后发送给用户。

1. 解压(或解密)

演示代码如下,解压使用 icsharpcode.sharpziplib.dll:

public actionresult filestreamdownload1() {var path = server.mappath("~/files/鹤冲天.zip");var filestream = new filestream(path, filemode.open);var zipinputstream = new zipinputstream(filestream);var entry = zipinputstream.getnextentry();return file(zipinputstream, "application/pdf", url.encode(entry.name)); }

简单起见,假定压缩文件中只有一个文件,且是 pdf 格式的。鹤冲天.zip 如下:

点击后弹出下载提示窗口如下:

2. 转发(或盗链)

filestreamresult 的另一种用途是将其它网站上的文件作为本站文件下载(其实就是盗链):

public actionresult filestreamdownload1() {var stream = new webclient().openread("http://files.cnblogs.com/ldp615/mvc_textboxfor.rar");return file(stream, "application/x-zip-compressed", "mvc_textboxfor.rar"); }

看下面提示窗口,来源还是 localhost:

总结

以上是尊龙游戏旗舰厅官网为你收集整理的asp.net mvc:通过 fileresult 向 浏览器 发送文件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图