Stream-based upload:
To upload binaries via stream, use FtpWebRequest
:
<code class="language-csharp">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:\local\path\file.zip")) using (Stream ftpStream = request.GetRequestStream()) { fileStream.CopyTo(ftpStream); }</code>
Stream-based downloads:
For streaming downloads, use FtpWebRequest
:
<code class="language-csharp">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:\local\path\file.zip")) { ftpStream.CopyTo(fileStream); }</code>
The above is the detailed content of How to Upload and Download Files via FTP in C# Using Streaming?. For more information, please follow other related articles on the PHP Chinese website!