• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETMVC:通过FileResult向浏览器发送文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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);
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"); 
}

 

参考文献:ASP.NET MVC:通过 FileResult 向 浏览器 发送文件


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap