通过文件流stream下载文件

本文介绍了一个用于展示特定ID对应的本地化XML文件的方法。通过此方法可以获取XML字符串,并将其作为附件形式提供给用户下载。文章详细展示了如何使用C#处理XML数据并设置HTTP响应以实现文件下载。
        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();
        }

 

转载于:https://www.cnblogs.com/shinima/p/4229012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值