I have this working code from this link, to upload a file to an ftp site:
' 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()
I wonder, if the file already exists in the ftp directory, the file will be overwritten?
Looking at the MSDN documentation this maps to the FTP STOR command. Looking at the definition for the FTP STOR command it will overwrite existing files, if the user has permissions.
So in this case, yes the file would be overwritten.
From: Link
STOR (STORE)
STOR
This command causes the FTP server to accept the data transferred via the data connection and to store the data as a file at the FTP server. If the file specified in pathname exists at the server site, then its contents shall be replaced by the data being transferred. A new file is created at the FTP server if the file specified in pathname does not already exist.
this is working code to upload files to a FTP server
It is important to know that, Files are just references to pointers that point to an array of bytes in memory.
When a file writing operation is asked to write a file to a pointer, it will not check if the file exist; Simply, the file system will allow the operation to continue unless the bytes in memory are being used (although you can force overwrite).
If you want to check if a file exist before writing the file use my GetDirectory method in VB.net here: https://stackoverflow.com/a/28664731/2701974
Yes, FTP protocol overwrites existing files on upload.
Note that there are better ways to implement the upload.
The most trivial way to upload a binary file to an FTP server using .NET framework is using
WebClient.UploadFile
:If you need a greater control, that
WebClient
does not offer (like TLS/SSL encryption, etc), useFtpWebRequest
. Easy way is to just copy aFileStream
to FTP stream usingStream.CopyTo
:If you need to monitor an upload progress, you have to copy the contents by chunks yourself:
For GUI progress (WinForms
ProgressBar
), see C# example at:How can we show progress bar for upload with FtpWebRequest
If you want to upload all files from a folder, see C# example at
Upload directory of files using WebClient.
Use This Function to Upload file
Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)
End Sub
HOW TO USE:
' Upload file using FTP UploadFile("c:\UploadFile.doc", "ftp://FTPHostName/UploadPath/UploadFile.doc", "UserName", "Password")