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

摘要:
原因是fileDownloadName将是URL的一部分,并且只能包含ASCII代码。因此,我们需要对Url进行编码。EncodedplicActionResultFilePathDownload4(){varpath=Server.MapPath;varname=Path.GetFileName;returnFile;}对于名称FileContentResultFileContentResult,可以将byte[]作为文件直接发送到浏览器publicFileResultDownload(){byte[]fileBytes=System.IO.file.ReadAllBytes;stringfileName=“myfile.txt”;returnFile;}对于FileStreamResult来说,要找到一个适用于FileStreamResult的示例并不容易。毕竟,HttpResponse已经包含OutputStream属性。如果要动态生成文件,可以直接将数据写入此输出流,这是非常高效的。当然,我们不会直接将数据写入控制器中响应的OutputStream。这不符合MVC。我们应该将此操作封装为ActionResult。

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:foldermyfile.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 向 浏览器 发送文件

免责声明:文章转载自《ASP.NET MVC:通过 FileResult 向 浏览器 发送文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux资源使用配置文件 /etc/security/limits.conf和ulimitios framework通用库的制作下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于Eclipse插件开发(四)-------给视图加下拉菜单和按钮和加入编辑器.

本例将给视图加入下拉菜单和按钮,同时再为列表添加一个右键菜单. 创建ActionGroup类 加入菜单和按钮的方法与SWT和JFace组件的一样,先创建一个ActionGroup代码如下:  MyActionGroup.java   1 public class MyActionGroup extends ActionGroup { 2 //...

Consul--客户端访问Consul服务

根据上一篇Consul服务的注册和发现,那么客户端如何去访问我们的Consul服务?其实客户端访问Consul实际上是访问Consul的服务实例。客户端自己可以实现对Consul服务实例的轮训,每次刷新端口都会发生改变,由于客户端访问Consul采用的轮训策略,所以每次刷新Consul的服务实例都会发生改变。 下面就直接上客户端访问Consul服务实例的代...

MongoDB配置文件YAML-based选项全解

配置文件部分 MongoDB引入一个YAML-based格式的配置文件。2.4版本以前的仍然兼容。 我的mongodb配置文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39...

word的常用操作

using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.IO;using System.Web;using System.Data;using System.Reflection;usin...

生成注册码的一种算法

public static string GetSN(string str){string strResult="";//作为返回的字符串StringBuilder sbTemp=new StringBuilder(); //生成注册码for (int i = 0; i < str.Length; i++){int tIn = str[i] % 10...

Java后台读取excel表格返回至Web前端

如果是做连接数据库的话,系统难度就降低了不少;这次本人也算是体会到数据库的方便了吧(不过以后云储存好像会更受欢迎些);比如说查询列出所有数据吧:数据库每个表每一列都有列名,正常的做法是遍历数据库表,dao层利用list储存实体对象集,数据库表中每一行记录一个实体的各个属性: public List<Account> list() {...