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
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") |