李维强-15级 发表于 2019-8-1 23:51:22

C#下载远程文件到本地

方法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)));
页: [1]
查看完整版本: C#下载远程文件到本地