Home >Backend Development >C++ >How to Upload and Download Files via FTP in C# Using Streaming?

How to Upload and Download Files via FTP in C# Using Streaming?

DDD
DDDOriginal
2025-01-11 11:10:42474browse

How to Upload and Download Files via FTP in C# Using Streaming?

C#/.NET FTP file upload and download (streaming)

Upload

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>

Download

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn