ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
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 fileFilePathResult
- 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.File
overloads 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.Encode public 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("https://files.cnblogs.com/level/test.rar"); return File(stream, "application/x-zip-compressed", "test.rar"); }