重工电子论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 5810|回复: 0

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

[复制链接]

286

主题

666

帖子

5517

积分

学生管理组

Rank: 8Rank: 8

积分
5517
发表于 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)));
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 21:40 , Processed in 0.148700 second(s), 29 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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