博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
阅读量:4326 次
发布时间:2019-06-06

本文共 3219 字,大约阅读时间需要 10 分钟。

FileResult is an abstract base class for all the others.

  • FileContentResult - you use it when you have a byte array you would like to return as a file
  • FilePathResult - when you have a file on disk and would like to return it's content (you give a path)
  • FileStreamResult - you have a stream open, you want to return it's content as a file

However, you'll rarely have to use these classes - you can just use one of Controller.Fileoverloads and let asp.net mvc do the magic for you.

 

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

public ActionResult FilePathDownload1(){    var path = Server.MapPath("~/Files/BarcodeConverter.exe");    return File(path, "application/x-zip-compressed");}public ActionResult FilePathDownload2(){    var path = Server.MapPath("~/Files/BarcodeConverter.exe");   return File("g:\\BarcodeConverter.exe", "application/x-zip-compressed", "BarcodeConverter.exe"); } public ActionResult FilePathDownload3(){    var path = Server.MapPath("~/Files/BarcodeConverter.exe");    var name = Path.GetFileName(path);     return File(path, "application/x-zip-compressed", name); }//FilePathDownload3  下载后的文件名还是默认为了 Action 的名字。原因是 fileDownloadName 将作为 URL 的一部分,只能包含 ASCII 码。所以,我们需要对name进行encode Url.Encodepublic ActionResult FilePathDownload4() {     var path = Server.MapPath("~/Files/BarcodeConverter.exe");     var name = Path.GetFileName(path);     return File(path, "application/x-zip-compressed",Url.Encode(name)); }

 

FileContentResult

FileContentResult 可以直接将 byte[] 以文件形式发送至浏览器(而不用创建临时文件)

public FileResult Download(){    byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.txt");    string fileName = "myfile.txt";    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);}

FileStreamResult

想给 FileStreamResult 找一个恰当的例子是不太容易的,毕竟 Http Response 中已经包含了一个OutputStream属性,

如果要动态生成文件的话,可以直接向这个输出流中写入数据,效率还高。

当然,我们不会在 Controller 中直接向 Response 的 OutputStream 写入数据,这样做是不符合MVC的,我们应该把这个操作封装成一个 ActionResult。

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

 (1)解压(或解密)

public ActionResult FileStreamDownload1(){    var path = Server.MapPath("~/Files/myfile.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 格式的。}

(2)转发(或盗链)

将其它网站上的文件作为本站文件下载(其实就是盗链):
public ActionResult FileStreamDownload1(){    var stream = new WebClient().OpenRead("http://files.cnblogs.com/level/test.rar");    return File(stream, "application/x-zip-compressed", "test.rar"); }

 

参考文献:

转载于:https://www.cnblogs.com/wxlevel/p/6866278.html

你可能感兴趣的文章
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
迷宫实现
查看>>
【字符编码】Java字符编码详细解答及问题探讨
查看>>
学习操作系统导图
查看>>
在线的JSON formate工具
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
xml解析
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
查看>>
Python学习笔记-EXCEL操作
查看>>
CListCtrlEx:一个支持文件拖放和实时监视的列表控件——用未公开API函数实现Shell实时监视...
查看>>
DirectShow实现抓图(Delphi)
查看>>
PS3 可播放的多媒体类型
查看>>
游戏开发的调试机制
查看>>