码迷,mamicode.com
首页 > Web开发 > 详细

MVC下载文件方式

时间:2016-03-07 18:47:59      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

方式一:

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下载文件方式

标签:

原文地址:http://www.cnblogs.com/zooni/p/5251313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!