2012-01-10 50 views
8

FTP站點我從這個link這個工作代碼,將文件上傳到FTP站點:上傳文件使用VB.NET

' set up request... 
Dim clsRequest As System.Net.FtpWebRequest = _ 
    DirectCast(System.Net.WebRequest.Create("ftp://ftp.myserver.com/test.txt"), System.Net.FtpWebRequest) 
clsRequest.Credentials = New System.Net.NetworkCredential("myusername", "mypassword") 
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile 

' read in file... 
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\Temp\test.txt") 

' upload file... 
Dim clsStream As System.IO.Stream = _ 
    clsRequest.GetRequestStream() 
clsStream.Write(bFile, 0, bFile.Length) 
clsStream.Close() 
clsStream.Dispose() 

我不知道,如果該文件中的ftp目錄已經存在,該文件將被覆蓋?

+2

你爲什麼不試試? – Orentet 2012-01-10 19:28:35

+0

@Orentet到目前爲止我無法刪除/更新ftp,我只想繼續前進,看看會發生什麼情況。 – Somebody 2012-01-10 19:39:09

+0

奇怪的是,我只是建立了自己的文件上傳根據您的代碼,我得到了WebException(530)沒有登錄... – Ortund 2013-03-18 14:56:50

回答

8

查看MSDN文檔,它映射到FTP STOR命令。查看FTP STOR命令的定義,如果用戶有權限,它將覆蓋現有文件。

所以在這種情況下,是的文件將被覆蓋。

3

來自:Link

STOR(STORE)

STOR

此命令使FTP服務器接受經由數據連接傳送的數據且將所述數據存儲爲在FTP文件服務器。如果路徑名中指定的文件存在於服務器站點,則其內容應由正在傳輸的數據替換。如果路徑名中指定的文件尚不存在,則會在FTP服務器上創建一個新文件。

-1

重要的是要知道, 文件只是指向指向內存中的字節數組的指針。

當文件寫入操作被要求將文件寫入指針時,它不會檢查文件是否存在;簡單地說,文件系統將允許操作繼續,除非內存中的字節被使用(,儘管你可以強制覆蓋)。

如果您想檢查文件中寫入該文件用我GetDirectory方法在VB.net這裏之前存在:https://stackoverflow.com/a/28664731/2701974

-1

使用這個功能來上傳文件

公用Sub UploadFile(BYVAL _filename作爲字符串,BYVAL _UploadPath作爲字符串,BYVAL _FTPUser作爲字符串,BYVAL _FTPPass作爲字符串)

昏暗_FileInfo作爲新System.IO.FileInfo(_filename)

' Create FtpWebRequest object from the Uri provided 
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest) 

' Provide the WebPermission Credintials 
_FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass) 

' By default KeepAlive is true, where the control connection is not closed 
' after a command is executed. 
_FtpWebRequest.KeepAlive = False 

' set timeout for 20 seconds 
_FtpWebRequest.Timeout = 20000 

' Specify the command to be executed. 
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile 

' Specify the data transfer type. 
_FtpWebRequest.UseBinary = True 

' Notify the server about the size of the uploaded file 
_FtpWebRequest.ContentLength = _FileInfo.Length 

' The buffer size is set to 2kb 
Dim buffLength As Integer = 2048 
Dim buff(buffLength - 1) As Byte 

' Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead() 

Try 
    ' Stream to which the file to be upload is written 
    Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream() 

    ' Read from the file stream 2kb at a time 
    Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength) 

    ' Till Stream content ends 
    Do While contentLen <> 0 
     ' Write Content from the file stream to the FTP Upload Stream 
     _Stream.Write(buff, 0, contentLen) 
     contentLen = _FileStream.Read(buff, 0, buffLength) 
    Loop 

    ' Close the file stream and the Request Stream 
    _Stream.Close() 
    _Stream.Dispose() 
    _FileStream.Close() 
    _FileStream.Dispose() 
Catch ex As Exception 
    MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
End Try 

末次

如何使用:

「使用FTP上傳 文件UploadFile( 」C:\ UploadFile.doc「, 」ftp://FTPHostName/UploadPath/UploadFile.doc「, 」用戶名「, 」密碼「)

0

是,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