how to ftp a file to a server using VB.Net

I have a file with full path = "c:\myfolder\myfile.pdf"
and my ftp server is = "ftp.mydomain.com"
my ftp user id is = "mydomain.com"
my ftp password is = "myPassword"
what I need to do is to write a vb.net code to send the file from my local drive to the folder "/uploads" on my server, and if the folder does not exist I need to create it.
Please provide vb.net code.
Help please. Thanks
fmichailAsked:
Who is Participating?
 
HairbrushConnect With a Mentor Commented:
I have written this for you; I think it will do exactly what you ask...
Imports System.Net
Imports System.IO

Module Module1

    Sub Main()

        Dim PDFFile As New FileInfo("c:\myfolder\myfile.pdf")
        Dim Folder As New Uri("ftp://ftp.mydomain.com/uploads/")
        Dim Uri As New Uri(Folder, PDFFile.Name)
        Dim Credential As New NetworkCredential("mydomain.com", "myPassword")
        Dim UsePassive As Boolean = True

        CreateFolderIfDoesNotExist(Folder, Credential, UsePassive)
        Upload(PDFFile, Uri, Credential, UsePassive)

    End Sub

    Sub Upload(FileToUpload As FileInfo, ByVal Destination As Uri, ByVal Credential As NetworkCredential, ByVal UsePassive As Boolean)

        Dim ftp As FtpWebRequest = GetRequest(Destination, Credential, UsePassive)
        With ftp
            .Method = WebRequestMethods.Ftp.UploadFile
            .UseBinary = True
            .ContentLength = FileToUpload.Length
        End With

        'create byte array to store: ensure at least 1 byte!
        Const BufferSize As Integer = 2048
        Dim content(BufferSize - 1) As Byte, dataRead As Integer

        'open file for reading 
        Using fs As FileStream = FileToUpload.OpenRead()
            Try
                'open request to send
                Using rs As Stream = ftp.GetRequestStream
                    Do
                        dataRead = fs.Read(content, 0, BufferSize)
                        rs.Write(content, 0, dataRead)
                    Loop Until dataRead < BufferSize
                    rs.Close()
                End Using
            Catch ex As Exception

            Finally
                fs.Close() 'ensure file closed
            End Try

        End Using

    End Sub

    Function GetRequest(ByVal URI As Uri, ByVal Credential As NetworkCredential, ByVal UsePassive As Boolean) As FtpWebRequest

        Dim result As FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
        With result
            .Credentials = Credential
            .KeepAlive = False
            .UsePassive = UsePassive
        End With
        Return result

    End Function

    Function GetStringResponse(ByVal ftp As FtpWebRequest) As String
        'Get the result, streaming to a string
        Dim result As String = ""
        Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
            Dim size As Long = response.ContentLength
            Using datastream As Stream = response.GetResponseStream
                Using sr As New StreamReader(datastream)
                    result = sr.ReadToEnd()
                    sr.Close()
                End Using
                datastream.Close()
            End Using
            response.Close()
        End Using
        Return result
    End Function

    Sub CreateFolderIfDoesNotExist(ByVal Folder As Uri, ByVal Credential As NetworkCredential, ByVal UsePassive As Boolean)
        Try
            Dim request As FtpWebRequest = GetRequest(Folder, Credential, UsePassive)
            request.Method = WebRequestMethods.Ftp.ListDirectory
            Using response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
                Exit Sub 'Folder exists 
            End Using
        Catch ex As WebException
            If Not ex.Response Is Nothing Then
                Dim response As FtpWebResponse = CType(ex.Response, FtpWebResponse)
                If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
                    'Folder does not exist, so create it
                    Dim ftp As FtpWebRequest = GetRequest(Folder, Credential, UsePassive)
                    ftp.Method = WebRequestMethods.Ftp.MakeDirectory

                    Try
                        'get response but ignore it
                        Dim str As String = GetStringResponse(ftp)
                    Catch ex2 As Exception
                        Throw
                    End Try
                End If
            End If
        End Try
    End Sub

End Module

Open in new window

0
 
rawinnlnx9Connect With a Mentor Commented:
Your Google is a bit weak but here you go: http://www.codeproject.com/KB/IP/FtpClient.aspx (a tutorial of your question).
0
 
fmichailAuthor Commented:
Great answers, Thank you both
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.