mercredi 28 septembre 2016

How To Upload and Download Files with FTP from a VB.NET Application

In these days of Azure and The Cloud this post might seem to be a bit out of date, but if you want to access files on your web hosting service then here’s how you can do it with FTP.
Uploading Here’s a method that takes the four key pieces of information:
The name of the file to be uploaded
The web address of the ftp server on your web host
Your FTP username on your web host
The FTP Password
I’ve been surprised at how many examples only cover cases where Anonymous FTP is allowed.  I’m not sure that’s very realistic in a scenario where you are uploading your personal files.
Anyway, here’s the method that takes those four parameters:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19






    Private Sub FtpUploadFile(ByVal filetoupload As String, ByVal ftpuri As String, ByVal ftpusername As String, ByVal ftppassword As String)
        ' Create a web request that will be used to talk with the server and set the request method to upload a file by ftp.
        Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpuri), FtpWebRequest)

        Try
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile

            ' Confirm the Network credentials based on the user name and password passed in.
            ftpRequest.Credentials = New NetworkCredential(ftpusername, ftppassword)

            ' Read into a Byte array the contents of the file to be uploaded 
            Dim bytes() As Byte = System.IO.File.ReadAllBytes(filetoupload)

            ' Transfer the byte array contents into the request stream, write and then close when done.
            ftpRequest.ContentLength = bytes.Length
            Using UploadStream As Stream = ftpRequest.GetRequestStream()
                UploadStream.Write(bytes, 0, bytes.Length)
                UploadStream.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

        MessageBox.Show("Process Complete")
End Sub
As you can see from the comments, a WebRequest is created and the FTP UploadFile method is called.  Then the web credentials are passed in to be checked by the web host.  The contents of the file are read into a Byte Array and then streamed up to the server.  The Try Catch block is advisable to warn the user if something has gone wrong.  The final Message Box confirms that the process has completed, hopefully successfully.
When you create your method to call the method shown above, the only thing that might cause confusion is the ftpuri parameter.  This should in the following format:
     “ftp://www.yourwebsitename.com/yourfilename.fileextension”
Downloading The method for downloading is fairly similar:
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


    Private Sub FTPDownloadFile(ByVal downloadpath As String, ByVal ftpuri As String, ByVal ftpusername As String, ByVal ftppassword As String)
        'Create a WebClient.
        Dim request As New WebClient()

        ' Confirm the Network credentials based on the user name and password passed in.
        request.Credentials = New NetworkCredential(ftpusername, ftppassword)

        'Read the file data into a Byte array
        Dim bytes() As Byte = request.DownloadData(ftpuri)

        Try
            '  Create a FileStream to read the file into
            Dim DownloadStream As FileStream = IO.File.Create(downloadpath)
            '  Stream this data into the file
            DownloadStream.Write(bytes, 0, bytes.Length)
            '  Close the FileStream
            DownloadStream.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try

        MessageBox.Show("Process Complete")

    End Sub