是,FTP協議會覆蓋上載的現有文件。
請注意,有更好的方法來實現上傳。
將二進制文件上傳到FTP服務器的最簡單的方法。.NET框架是使用WebClient.UploadFile
:如果你需要一個更大的控制
Dim client As WebClient = New WebClient
client.Credentials = New NetworkCredential("username", "password")
client.UploadFile("ftp://ftp.example.com/remote/path/file.zip", "C:\local\path\file.zip")
,即WebClient
不提供(如TLS/SSL加密等),使用FtpWebRequest
。簡單的方法是使用Stream.CopyTo
只是複製FileStream
到FTP流:
Dim request As FtpWebRequest =
WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.UploadFile
Using fileStream As Stream = File.OpenRead("C:\local\path\file.zip"),
ftpStream As Stream = request.GetRequestStream()
fileStream.CopyTo(ftpStream)
End Using
如果你需要監視的上傳進度,你必須塊自己的內容複製:
Dim request As FtpWebRequest =
WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.UploadFile
Using fileStream As Stream = File.OpenRead("C:\local\path\file.zip"),
ftpStream As Stream = request.GetRequestStream()
Dim read As Integer
Do
Dim buffer() As Byte = New Byte(10240) {}
read = fileStream.Read(buffer, 0, buffer.Length)
If read > 0 Then
ftpStream.Write(buffer, 0, read)
Console.WriteLine("Uploaded {0} bytes", fileStream.Position)
End If
Loop While read > 0
End Using
對於GUI進度(的WinForms ProgressBar
),看到的C#示例:
How can we show progress bar for upload with FtpWebRequest
如果你想將所有文件從一個文件夾上傳,請參閱C#示例 Upload directory of files using WebClient。
你爲什麼不試試? – Orentet 2012-01-10 19:28:35
@Orentet到目前爲止我無法刪除/更新ftp,我只想繼續前進,看看會發生什麼情況。 – Somebody 2012-01-10 19:39:09
奇怪的是,我只是建立了自己的文件上傳根據您的代碼,我得到了WebException(530)沒有登錄... – Ortund 2013-03-18 14:56:50