首页 >后端开发 >C++ >如何在C#中使用流式传输通过FTP上传和下载文件?

如何在C#中使用流式传输通过FTP上传和下载文件?

DDD
DDD原创
2025-01-11 11:10:42474浏览

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

C#/.NET FTP 文件上传和下载(流式处理)

上传

基于流的上传:

要通过流上传二进制文件,请使用 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>

下载

基于流的下载:

对于流式下载,请使用 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>

以上是如何在C#中使用流式传输通过FTP上传和下载文件?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn