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

通过文件流stream下载文件

时间:2015-01-16 18:44:48      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

        public ActionResult ShowLocalizedXML(int id)
        {
            string orderName = "";
            string xmlString = GetXmlStream(id,out orderName);
            ViewBag.Xml = xmlString;

            XmlDocument doc = new XmlDocument();
            doc.CreateComment(xmlString);

            byte[] array = Encoding.UTF8.GetBytes(xmlString);
           //array = UnicodeEncoding.Convert(Encoding.UTF8, Encoding.Unicode, array);
            MemoryStream stream = new MemoryStream(array);  

            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment; filename="+orderName+".xml");//Filename改成什么格式,就会下载什么格式文件
            Response.AppendHeader("Content-Length", "" + stream.Length);

            byte[] bts = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(bts, 0, (int)stream.Length);

            Response.OutputStream.Write(bts, 0, bts.Length);
            return View();
        }

 

通过文件流stream下载文件

标签:

原文地址:http://www.cnblogs.com/shinima/p/4229012.html

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