博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpWebRequest(跨域下载文件——网络流转换为内存流下载)
阅读量:7029 次
发布时间:2019-06-28

本文共 2964 字,大约阅读时间需要 9 分钟。

1.Stream 转换为 MemoryStream(Stream不支持查找)

MemoryStream StreamToMemoryStream(Stream instream)        {            MemoryStream outstream = new MemoryStream();            const int bufferLen = 4096;            byte[] buffer = new byte[bufferLen];            int count = 0;            while ((count = instream.Read(buffer, 0, bufferLen)) > 0)            {                outstream.Write(buffer, 0, count);            }            return outstream;        }
View Code

2.网络流转化为内存流并下载

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filePath);                        request.Method = "GET";                        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)                        {                            if (response.StatusCode == HttpStatusCode.OK)                            {                                Stream rs = response.GetResponseStream();//网络流转换为内存流                                var ms = StreamToMemoryStream(rs);                                ms.Seek(0, SeekOrigin.Begin); int buffsize = (int)ms.Length; //rs.Length 此流不支持查找,先转为MemoryStream                                byte[] bytes = new byte[buffsize];                                ms.Read(bytes, 0, buffsize);                                ms.Flush(); ms.Close();                                rs.Flush(); rs.Close();//以文件流的方式下载                                Response.Charset = "utf-8";                                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");                                Response.ContentType = "application/octet-stream";                                if (HttpContext.Current.Request.UserAgent.ToUpper().Contains("MSIE") ||                                    HttpContext.Current.Request.UserAgent.ToUpper().Contains("TRIDENT") ||                                    HttpContext.Current.Request.UserAgent.ToUpper().Contains("EDGE"))                                {                                    name = HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8);                                }                                else                                {                                    name = name.Replace(" ", "_");                                }                                //fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);                                Response.AddHeader("Content-Disposition", "attachment;   filename=" + name);                                Response.BinaryWrite(bytes);                                Response.Flush();                                Response.End();                            }                        }
View Code

3.网络路径

private string GetContractPathRules(string id, string name) {            string url = ConfigurationManager.AppSettings["ContractUrl"];            url = url.TrimEnd('/') + "/";            return Path.Combine(url, Path.Combine(id + "/", name));        }//例如:http://x.x.x.x:8088/xxx/xxx.pdf
View Code

 

转载地址:http://cwwal.baihongyu.com/

你可能感兴趣的文章
JAVA訪问URL
查看>>
APP接口基础学习一
查看>>
设计模式 策略模式 以角色游戏为背景
查看>>
【转】CSS和SVG中的剪切——clip-path属性和<clipPath>元素
查看>>
【C语言入门教程】5.4 递归
查看>>
UVALive 6915 Leveling Ground 倍增RMQ
查看>>
Inside ARC — to see the code inserted by the compiler
查看>>
云中气象 有备而来
查看>>
4.dubbo-demo+简易监控中心安装+管理控制台安装
查看>>
读书笔记《集体智慧编程》Chapter 4 : Searching and Ranking
查看>>
jquery form 插件 分类: JavaScript ...
查看>>
php二维数组访问
查看>>
用Shell实现俄罗斯方块代码(Tetris.sh)
查看>>
[zz]Ubuntu Hadoop HDFS 配置
查看>>
上市后Avaya锣鼓全开,加速战略布局规划
查看>>
日调度5万亿次,腾讯云发布企业级微服务中间件TSF
查看>>
海外侨胞建言四川对外开放:加强内陆省份竞争力成关键
查看>>
2019款奥迪Q7上市 配置增加/69.98万元起售
查看>>
策划求婚、陪挑婚纱,新郎不是我,仍感谢你来过|在百度遇见你
查看>>
从零单排学Redis【铂金一】
查看>>