首页 > Web开发 > 详细

MVC 之下载 我的实践

时间:2016-11-19 20:51:05      阅读:171      评论:0      收藏:0      [点我收藏+]

Controller

1、

public ActionResult DownLoad(string path,string fileName)

{

return File(new FileStream(path, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));

}

2、

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 

 

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

 

MVC 之下载 我的实践

原文:http://www.cnblogs.com/DataBase-123/p/6081136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!