码迷,mamicode.com
首页 > 其他好文 > 详细

返回流的形式下载文件

时间:2017-08-04 18:20:57      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:ica   文件   code   ppa   path   .text   isnull   coding   string   

 //下载文件
    private void Down(HttpContext context)
    {
        string filePath = context.Request["url"];
        if (!string.IsNullOrEmpty(filePath))
        {
            string customFileName = DateTime.Now.ToString("yyyyMMddHHmmss");//客户端保存的文件名  
            using (FileStream fileStream = new FileStream(Server.MapPath(filePath), FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] fileData = new byte[fileStream.Length];
                fileStream.Read(fileData, 0, fileData.Length);
                string fileName = Path.GetFileNameWithoutExtension(filePath);
                                                //attachment 作为附件打开 inline 在线打开
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString());
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
                HttpContext.Current.Response.BinaryWrite(fileData);
            }
        }
    }

 

返回流的形式下载文件

标签:ica   文件   code   ppa   path   .text   isnull   coding   string   

原文地址:http://www.cnblogs.com/dwtMyBlog521/p/7286294.html

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