通过文件流stream下载文件

        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();
        }

 

posted @ 2015-01-16 16:04  小姐,请叫我“序员”好吗  阅读(265)  评论(0编辑  收藏  举报