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 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.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("http://files.cnblogs.com/level/test.rar");
    return File(stream, "application/x-zip-compressed", "test.rar"); 
}

 

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

关键词:file path 文档 string var you return nbsp public application

相关推荐:

html2pdf 网页转PDF

【ASP.NET MVC系列】浅谈ASP.NET MVC八大类扩展(上篇)

asp.net mvc Controller控制器返回类型

asp.net mvc导出execl_转载

ASP.NET MVC中常用的ActionResult类型

MVC教程五:Action方法的返回类型

Asp.net mvc 中View的呈现(一)

mvc文档上传与下载

第六章 MVC之 FileResult和JS请求二进制文档