MVC下载文件方式
方式一:
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();
}

mvc下载文件的更多相关文章

  1. MVC下载文件方式

    MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...

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

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

  3. spring MVC 下载文件(转)

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

  4. Asp.net mvc 下载文件

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

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

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

  6. Spring MVC -- 下载文件

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

  7. spring mvc 下载文件链接

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

  8. Spring mvc 下载文件处理

    @RequestMapping(value = "downFile") public void downFile(HttpServletResponse response, Str ...

  9. Spring mvc下载文件java代码

    /** * 下载模板文件 * @author cq */ @RequestMapping("/downloadExcel.do") public ResponseEntity< ...

随机推荐

  1. 【Daily】 2014-4-28

    KEEP GOING  表达产品想法, 探讨产品问题, 倾听可能性问题. 一次就做好, 有成果展示, 主动展示 先确立图, 后确立代码. Hold dream, and never let it go ...

  2. poj2924---高斯求和

    #include <stdio.h> #include <stdlib.h> int main() { ; long long ans,a,b; scanf("%d& ...

  3. 实现多文件上传在iOS开发中

    该功能实现了实现多文件上传在iOS开发中,喜欢的朋友可以研究一下吧. NSURL* url = [NSURL URLWithString:@"xxx"]; ASIFormDataR ...

  4. OC中ARC forbids explicit message send of release错误(转)

    ARC forbids explicit message send of'release' 很显然,是ARC的问题. 错误原因:在创建工程的时候点选了“Use Automatic Reference ...

  5. 数据挖掘(七):Apriori算法:频繁模式挖掘

    1 算法思想 算法使用频繁项集性质的先验知识.Apriori使用一种称作逐层搜索的迭代方法,k项集用于探索(k+1)项集.首先,通过扫描数据库,累积每个项的计数,并收集满足最小支持度的项,找出频繁1项 ...

  6. Android常用控件之GridView使用BaseAdapter

    我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...

  7. Java面试题Tomcat的优化经验

    一.掉对web.xml的监视,把jsp提前编辑成Servlet.有富余物理内存的情况,加大tomcat使用的jvm的内存 二.服务器资源 服务器所能提供CPU.内存.硬盘的性能对处理能力有决定性影响. ...

  8. 执行SQL存储脚本

    using System.Data.SqlClient; static void Main(string[] args) { string connString = @"Data Sourc ...

  9. meta标签的少许语法,慢慢收集中...

    收集了一些meta的语法,也将不断的更新.不断做点滴的收集,总之,为了前端这些东西呀,我也是操碎了心... 1 <meta http-equiv="Content-Type" ...

  10. Beyond Compare设置默认为ANSI格式

    工具 -> 文件格式 -> 选中C,C++,... -> 转换 -> 外部程序(ANSI文件名) 且 编码(选“ANSI”)-> 保存 -> 关闭