重工电子论坛

标题: C#下载远程文件到本地 [打印本页]

作者: 李维强-15级    时间: 2019-8-1 23:51
标题: C#下载远程文件到本地
[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)));





欢迎光临 重工电子论坛 (http://cqutlab.cn/) Powered by Discuz! X3.1