Visual Basic Code Snippet - Upload file to FTP Server

Visual Basic Code Snippet - Upload file to FTP Server

Visual Basic Code Snippet - Upload file to FTP Server

(VB) Visual Basic code snippet upload file to FTP Server. This method login to FTP server using username and password and upload the local file to given FTP location.

Bookmark:

Visual Basic Code Snippet - Upload file to FTP Server

This .Net Visual Basic code snippet upload file to FTP Server. To use this function simply provide the local file name to upload, upload path including host name, FTP username and FTP password. This function uses System.Net and System.IO namespaces to upload the file.

To upload file we create two streams, one for the FTP connection and another for the file we are reading from local disk to upload. These are the steps we take to upload a file: Create a FtpWebRequest object, set the FtpWebRequest.Method property to UploadFile, set the FtpWebRequest.Credentials property to our login information, get the request stream of the FtpWebRequest object. This is the stream we will write to, open the file we are going to upload and get a stream to its data, read the file data from the input stream and write it into the request stream and close the uploaded file stream and the request stream.

''' <summary>
''' Methods to upload file to FTP Server
''' </summary>
''' <param name="_FileName">local source file name</param>
''' <param name="_UploadPath">Upload FTP path including Host name</param>
''' <param name="_FTPUser">FTP login username</param>
''' <param name="_FTPPass">FTP login password</param>
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)

    ' 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
End Sub


Here is a simple example showing how to use above method (UploadFile) to upload file to FTP Server.

' Upload file using FTP
UploadFile("c:\UploadFile.doc", "ftp://FTPHostName/UploadPath/UploadFile.doc", "UserName", "Password")


VB Keywords Used:

  • FileInfo
  • FtpWebRequest
  • Credentials
  • KeepAlive
  • Timeout
  • UploadFile
  • UseBinary
  • ContentLength
  • FileStream
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, VB, Visual Basic, CLI, Upload File, FTP Server, FTP Upload, FTP, FTP Web Request, File Stream
  • Programming Language : Visual Basic (VB)

External Resources:

Harshit :: December 17-2009 :: 10:46 AM

Great Code Its Working!!!!!!!!!!!

Mario :: June 16-2010 :: 05:42 PM

Very valuable code for me as a beginner. Thanks a lot!! (tested and works in VB 2010)

Don :: October 02-2011 :: 03:22 PM

How to use this with VBA is MS Excel?

Gary :: October 03-2011 :: 04:26 PM

Trying to apply this to a database upload. can you help me in this regard?  Thank you

Leave a comment