惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
IT之家
IT之家
博客园 - 叶小钗
雷峰网
雷峰网
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美

博客园 - liang--liang

项目管理--风险管理 设置全局theme及读取theme方法 项目经理面试题 项目团队发展经历的几个阶段及其特点 软件开发模式简介 通过DataTable 返回Json格式,用于绑定Easyui tree 把DataTable 转换成Json格式,适用于EasyUI 绑定DataGrid c# 把数据表DataTable 导出到Excel 解决document.body.clientWidth 或 document.body.clientHeight 为0的问题 jquery Uploadify 在MVC3中的应用上传 IO Error 解决 基于jquery的上传插件Uploadify 3.1.1在MVC3中的使用 ORACLE中一个字符占多少字节? 软件开发:需求分析的20条法则(转) PowerDesigner PDM 生成SQL脚本 去除双引号方法 PowerDesigner 物理数据模型(PDM) 说明 常见Oracle HINT的用法 Oracle 分区表 总结 《XML实用大全》二 《XML实用大全》一
MVC下载文件方式
liang--liang · 2012-10-20 · via 博客园 - liang--liang

方式一:

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>