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
Release: 2025-01-11 11:10:42
Original
473 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template