博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC学习四:通过FileResult向浏览器发送文件
阅读量:2386 次
发布时间:2019-05-10

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

补充实例:

       public  ActionResult FilePathDownloadDemo()

        {
            string filePath = Server.MapPath(@"../Files/global.png");
            string fileName = Path.GetFileName(filePath);

            return File(filePath, "application/png", fileName);

        }

        public ActionResult FileContentDownloadDemo()

        {
            //byte[] data = Encoding.UTF8.GetBytes("欢迎访问 鹤冲天 的博客 ");
            //return File(data, "text/plain", "welcome.txt");

            string filePath = Server.MapPath(@"../Files/global.png");;

            byte[] data;
            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                Int32 dataLength = Convert.ToInt32(fileStream.Length);
                data = new byte[dataLength];
                fileStream.Read(data, 0, dataLength);
            }

            return File(data, "image/png", "FileName.png");

        }

        public ActionResult FileStreamDownloaDemo()

        {
            string filePath = Server.MapPath(@"../Files/global.png");
            var fileStream = new FileStream(filePath, FileMode.Open);
            return File(fileStream, "application/png", Url.Encode("自定义名称.png"));
        }

 

 

原文:

在 Controller 中我们可以使用 FileResult 向客户端发送文件。@ itxyz.net

FileResult 是一个抽象类,继承自 ActionResult。在 System.Web.Mvc.dll 中,它有如上三个子类,分别以不同的方式向客户端发送文件。@ itxyz.net

在实际使用中我们通常不需要直接实例化一个 FileResult 的子类,因为 Controller 类已经提供了六个 File 方法来简化我们的操作:

@ itxyz.net

protected internal FilePathResult File(string fileName, string contentType);
protected internal virtual FilePathResult File(string fileName, string contentType, stringfileDownloadName);
protected internal FileContentResult File(byte[] fileContents, string contentType);
protected internal virtual FileContentResult File(byte[] fileContents, string contentType,string fileDownloadName);@ itxyz.net
protected internal FileStreamResult File(Stream fileStream, string contentType);
protected internal virtual FileStreamResult File(Stream fileStream, string contentType,string fileDownloadName);

FilePathResult

FilePathResult 直接将磁盘上的文件发送至浏览器:

1. 最简单的方式

public ActionResult FilePathDownload1()
{
    var path = Server.MapPath("~/Files/鹤冲天.zip");
    return File(path, "application/x-zip-compressed");
}

第一个参数指定文件路径,第二个参数指定文件的 MIME 类型。用户点击浏览器上的下载链接后,会调出下载窗口: @ itxyz.net

 @ itxyz.net

大家应该注意到,文件名称会变成 Download1.zip,默认成了 Action 的名字。我们使用 File 方法的第二个重载来解决文件名的问题:

2. 指定 fileDownloadName

public ActionResult FilePathDownload2()
{
    var path = Server.MapPath("~/Files/鹤冲天.zip"); 
    return File("g:\\鹤冲天.zip", "application/x-zip-compressed", "crane.zip");
}
public ActionResult FilePathDownload3()
{
    var path = Server.MapPath("~/Files/鹤冲天.zip"); 
    var name = Path.GetFileName(path);
    return File(path, "application/x-zip-compressed", name);
}

我们可以通过给 fileDownloadName 参数传值来指定文件名,fileDownloadName 不必和磁盘上的文件名一样。下载提示窗口分别如下: @ itxyz.net  @ itxyz.net

FilePathDownload2 没问题,FilePathDownload3 还是默认为了 Action 的名字。原因是 fileDownloadName 将作为 URL 的一部分,只能包含 ASCII 码。我们把 FilePathDownload3 改进一下:

3. 对 fileDownloadName 进行 Url 编码 @ itxyz.net

public ActionResult FilePathDownload4() { var path = Server.MapPath("~/Files/鹤冲天.zip");var name = Path.GetFileName(path); return File(path, "application/x-zip-compressed", Url.Encode(name)); }

 

再试下,下载窗口如下:

@ itxyz.net

好了,没问题了。上面代码中 Url.Encode(…),也可使用 HttpUtility.UrlEncode(…),前者在内部调用后者。 @ itxyz.net

我们再来看 FileContentResult.

FileContentResult @ itxyz.net

FileContentResult 可以直接将 byte[] 以文件形式发送至浏览器(而不用创建临时文件)。参考代码如下:

public ActionResult FileContentDownload1() { byte[] data = Encoding.UTF8.GetBytes("欢迎访问 鹤冲天 的博客 http://www.cnblogs.com/ldp615/");return File(data, "text/plain", "welcome.txt"); }

点击后下载链接后,弹出提示窗口如下:

想给 FileStreamResult 找一个恰当的例子是不太容易的,毕竟 Http Response 中已经包含了一个输出流,如果要动态生成文件的话,可以直接向这个输出流中写入数据,效率还高。当然,我们不会在 Controller 中直接向 Response 的 OutputStream 写入数据,这样做是不符合MVC的,我们应该把这个操作封装成一个 ActionResult。

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

1. 解压(或解密)

演示代码如下,解压使用 ICSharpCode.SharpZipLib.dll:

public ActionResult FileStreamDownload1() @ itxyz.net
{
    var path = Server.MapPath("~/Files/鹤冲天.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 格式的。鹤冲天.zip 如下:

点击后弹出下载提示窗口如下:  @ itxyz.net

2. 转发(或盗链) @ itxyz.net

FileStreamResult 的另一种用途是将其它网站上的文件作为本站文件下载(其实就是盗链): @ itxyz.net

public ActionResult FileStreamDownload1() { var stream =new WebClient().OpenRead("http://files.cnblogs.com/ldp615/Mvc_TextBoxFor.rar");return File(stream, "application/x-zip-compressed", "Mvc_TextBoxFor.rar"); }

看下面提示窗口,来源还是 localhost:

         原文:http://www.cnblogs.com/ldp615/archive/2010/09/17/asp-net-mvc-file-result.html

 

 

你可能感兴趣的文章
squid缓存策略
查看>>
Linux Network Tuning
查看>>
二进制代码审核
查看>>
Grep与web漏洞挖掘
查看>>
libpcap 编程入门资源
查看>>
LFI2RCE (Local File Inclusion to Remote Code Execution) advanced exploitation: /proc shortcuts
查看>>
利用xss偷cookie教学
查看>>
pfSense 防火墙硬件平台性能评估指导手册
查看>>
Nagios statuswml.cgi远程Shell命令注入漏洞
查看>>
牛X人牛X事
查看>>
关于XSS Proxy技术
查看>>
漏洞扫描工具nikto使用心得
查看>>
Bash data exfiltration through DNS (using bash builtin functions)
查看>>
11 open source security tools catching fire on GitHub
查看>>
malware analysis
查看>>
盘点互联网巨头奉献的十大开源安全工具
查看>>
honeynet project tools
查看>>
Cisco路由器上防止DDOS的一些建议
查看>>
系统安全防护之UNIX下入侵检测方法
查看>>
域控渗透技巧
查看>>