重工电子论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 6153|回复: 0
打印 上一主题 下一主题

[C#] C#下载远程文件到本地

[复制链接]

287

主题

668

帖子

5654

积分

学生管理组

Rank: 8Rank: 8

积分
5654
跳转到指定楼层
楼主
发表于 2019-8-1 23:51:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode
方法1:WebClient 读取文件流下载

 WebClient client = new WebClient();

            byte[] bytes = client.DownloadData(Url);

          

            HttpContext.Response.ContentType = "application/octet-stream";

            HttpContext.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(Path.GetFileName(Url)));

            HttpContext.Response.AddHeader("Content-Length", bytes.Length.ToString());

            HttpContext.Response.OutputStream.Write(bytes, 0, bytes.Length);

            HttpContext.Response.Flush();

            HttpContext.Response.Clear();

方法2:WebClient 直接下载到指定文件夹

            WebClient client = new WebClient();

            client.DownloadFile(Url, @"D:\"+HttpUtility.UrlEncode(Path.GetFileName(Url)));
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|cqutlab ( 渝ICP备15004556号

GMT+8, 2024-5-14 19:19 , Processed in 0.161934 second(s), 30 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表