栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在C#/。NET中向FTP服务器上载二进制文件或从FTP服务器下载二进制文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在C#/。NET中向FTP服务器上载二进制文件或从FTP服务器下载二进制文件

上载

使用.NET框架将二进制文件上传到FTP服务器的最简单的方法是使用

WebClient.UploadFile

WebClient client = new WebClient();client.Credentials = new NetworkCredential("username", "password");client.UploadFile("ftp://ftp.example.com/remote/path/file.zip", @"C:localpathfile.zip");

如果您需要更大的控制权

WebClient
(例如TLS /
SSL加密
等),则无法使用,请使用
FtpWebRequest
。简单的方法是
FileStream
使用
Stream.CopyTo
以下命令将a复制到FTP流:

FtpWebRequest request =    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");request.Credentials = new NetworkCredential("username", "password");request.Method = WebRequestMethods.Ftp.UploadFile;using (Stream fileStream = File.OpenRead(@"C:localpathfile.zip"))using (Stream ftpStream = request.GetRequestStream()){    fileStream.CopyTo(ftpStream);}

如果需要监视上传进度,则必须自己逐块复制内容:

FtpWebRequest request =    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");request.Credentials = new NetworkCredential("username", "password");request.Method = WebRequestMethods.Ftp.UploadFile;using (Stream fileStream = File.OpenRead(@"C:localpathfile.zip"))using (Stream ftpStream = request.GetRequestStream()){    byte[] buffer = new byte[10240];    int read;    while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)    {        ftpStream.Write(buffer, 0, read);        Console.WriteLine("Uploaded {0} bytes", fileStream.Position);    } }

有关GUI进度(WinForms

ProgressBar
),请参见:
我们如何显示进度栏,以便使用FtpWebRequest上传

如果要从文件夹上传所有文件,请参阅
C#中的“递归上传到FTP服务器”。


下载

使用.NET框架从FTP服务器下载二进制文件的最简单的方法是使用

WebClient.DownloadFile

WebClient client = new WebClient();client.Credentials = new NetworkCredential("username", "password");client.DownloadFile(    "ftp://ftp.example.com/remote/path/file.zip", @"C:localpathfile.zip");

如果您需要更大的控制权

WebClient
(例如TLS /
SSL加密等),则无法使用,请使用
FtpWebRequest
。简单的方法是将FTP响应流复制到
FileStream
使用
Stream.CopyTo

FtpWebRequest request =    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");request.Credentials = new NetworkCredential("username", "password");request.Method = WebRequestMethods.Ftp.DownloadFile;using (Stream ftpStream = request.GetResponse().GetResponseStream())using (Stream fileStream = File.Create(@"C:localpathfile.zip")){    ftpStream.CopyTo(fileStream);}

如果需要监视下载进度,则必须自己逐块复制内容:

FtpWebRequest request =    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");request.Credentials = new NetworkCredential("username", "password");request.Method = WebRequestMethods.Ftp.DownloadFile;using (Stream ftpStream = request.GetResponse().GetResponseStream())using (Stream fileStream = File.Create(@"C:localpathfile.zip")){    byte[] buffer = new byte[10240];    int read;    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)    {        fileStream.Write(buffer, 0, read);        Console.WriteLine("Downloaded {0} bytes", fileStream.Position);    }}

有关GUI进度(WinForms

ProgressBar
),请参见:
FtpWebRequest使用ProgressBar下载FTP

如果要从远程文件夹
下载所有文件,请参见C#通过FTP下载所有文件和子目录。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号