MVC下载文件方式

http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html

方式一:

public FileStreamResult DownFile(string filePath, string fileName)
{
      string absoluFilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] +      filePath);
       return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
}

方式二:

public ActionResult DownFile(string filePath, string fileName)
{
filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return new EmptyResult();

}

View调用:

<a href="/Document/DownFile?filePath=@item.Value&fileName=@item.Key">下载</a>

MVC下载文件方式的更多相关文章

  1. MVC下载文件方式 包括网络地址文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){      string absol ...

  2. mvc下载文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName)  {       string ab ...

  3. 向linux服务器上传下载文件方式收集

    向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ...

  4. Spring MVC -- 下载文件

    像图片或者HTML文件这样的静态资源,在浏览器中打开正确的URL即可下载,只要该资源是放在应用程序的目录下,或者放在应用程序目录的子目录下,而不是放在WEB-INF下,tomcat服务器就会将该资源发 ...

  5. spring MVC 下载文件(转)

    springle MVC中如何下载文件呢? 比struts2 下载文件简单得多 先看例子: @ResponseBody @RequestMapping(value = "/download& ...

  6. Asp.net mvc 下载文件

    前言 最近有需求需要下载文件,可能是image的图片,也可能是pdf报告,也可能是微软的word或者excel文件. 这里就整理了asp.net mvc 和asp.net webapi 下载的方法 A ...

  7. 【第十三篇】mvc下载文件,包括配置xml保护服务端文件不被外链直接访问

    这里先说下载文件 <a style="color:black; margin-right:3px;" onclick="dowAtt(' + index + ')& ...

  8. spring mvc 下载文件链接

    http://www.blogjava.net/paulwong/archive/2014/10/29/419177.html http://www.iteye.com/topic/1125784 h ...

  9. ASP.NET 下载文件方式

    protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...

随机推荐

  1. ccc 旋转

    cc.Class({ extends: cc.Component, properties: { moveSpeed: 100, rotationSpeed: 90 }, // use this for ...

  2. BZOJ4382 : [POI2015]Podział naszyjnika

    对于每种颜色,可以发现可以切的位置被分割成了若干段独立的区域. 给每个区域一个编号,将$m$种颜色的情况当成字符串来看,如果两个切口的字符串完全匹配,那么可以在这里切两刀. 可以构造hash函数,通过 ...

  3. Redis的两个小技巧

    1.通配删除相同前缀的缓存 DEL命令的参数不支持通配符,但我们可以结合Linux的管道和xargs命令自己实现删除所有符合规则的键. 比如要删除所有以“structure_”开头的键,就可以执行下面 ...

  4. Compound Words

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=91209#problem/C 题意:   有一堆按照字典序排好的字符串,问你有多 ...

  5. HTTP协议及其请求头分析

    HTTP协议及其请求头分析 HTTP协议及其请求头分析   众所周知,Internet的基本协议是TCP/IP协议,目前广泛采用的FTP.Archie Gopher等是建立在TCP/IP协议之上的应用 ...

  6. 【资源】mp3的外链资源

    做开发有时候会遇到需要mp3外链来进行程序的调试,但是自己搭建服务器比较麻烦,而主流的音乐平台都不提供外链音乐.晚上偶得一外链mp3的网站,标记之: http://9hok.com/

  7. [CareerCup] 17.10 Encode XML 编码XML

    17.10 Since XML is very verbose, you are given a way of encoding it where each tag gets mapped to a ...

  8. Winform Combox DataSource 之不显示 displayemember 内容

    刚开始学习数据绑定的东西, private void Form1_Load(object sender, EventArgs e) { IList<TLayer> tt = new Lis ...

  9. 了解C++默认编写并调用哪些函数

    概念:编译器可以暗自为class创建default构造函数.copy构造函数.copy assignmengt构造函数,以及析构函数. 比如你写下:struct Node { }; 这就好像你写下这样 ...

  10. 常用js或jq效果汇总

    实时监控输入框改变    $('#password').bind('input propertychange', function() {}