基於流的上傳:
要透過串流上傳二進位文件,請使用 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中文網其他相關文章!