Hey everyone
I've been trying to get my ftp upload code to work for days now. I have created a vb.net application on my laptop (the client) but I'm also running the laptop as though it were a server (SQL Server for database and FileZilla for FTP Management).
I currently have a VB function called "UploadFileToFtp" that is meant to be able to connect to the "server" and upload (and download once I fix this) files. Here's my code for the function:
On a button I call the function and pass the specific parameters it requires. Below is just a simple test example, once I actually get the function to work correctly then I will change the file location "C:\blender_cube.obj" to be chosen via a file dialog. P.S. the last parameter ("data") is the folder name on the server that the file is meant to be uploaded to.Code:Public Function UploadFileToFtp(ByVal f As String, ByVal host As String, ByVal username As String, ByVal password As String, ByVal folderToUploadTo As String) As Boolean Try 'create an FtpRequest object Dim ftpRequest As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri(host + "/" + Path.GetFileName(folderToUploadTo))), FtpWebRequest) 'set the method to Upload, since we're uploading a file ftpRequest.Method = WebRequestMethods.Ftp.UploadFile 'pass in our login credentials ftpRequest.Credentials = New NetworkCredential(username, password) 'don't use passive mode ftpRequest.UsePassive = True 'unless we're uploading a file like an image 'we need to set this to false as we're not uploading 'binary data ftpRequest.UseBinary = True 'set KeepAlive to false ftpRequest.KeepAlive = False 'now create a new FileStream, then open the file we're uploading 'this allows us to get the size of the file for our buffer Dim stream As FileStream = File.OpenRead(f) 'create a byte[] array buffer the size of the file 'we're uploading Dim buffer As Byte() = New Byte(stream.Length - 1) {} 'read in our file into the FileStream stream.Read(buffer, 0, buffer.Length) 'close the FileStream stream.Close() 'create a new stream, this will be used to write to the 'FTP server Dim requestStream As Stream = ftpRequest.GetRequestStream() 'write the data to the FTP server requestStream.Write(buffer, 0, buffer.Length) 'close the stream requestStream.Close() 'since we made it this far return true Return True Catch ex As Exception 'something went wront so let the user know MessageBox.Show("Error uploading file: " + ex.Message, "Upload Error") 'return false Return False End Try End Function
I get the error "Invalid URI: The format of the URI could not be determined." on the first line of the function:Code:Private Sub btnModelView_UserDash_Click(sender As System.Object, e As System.EventArgs) Handles btnModelView_UserDash.Click UploadFileToFtp("C:\blender_cube.obj", "127.0.0.1", "Username", "Password", "data") End Sub
Can someone please help me, I've spent days looking at different ways and examples of how to do it from things like:Code:Dim ftpRequest As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri(host + "/" + Path.GetFileName(folderToUploadTo))), FtpWebRequest)
TOCode:My.Computer.Network.UploadFile("C:\blender_cube.obj", "ftp://127.0.0.1/data/", username, _ password, True, 100)
But every time I just come across more and more errors. I'm pretty set on this method (and like using functions) so any help would be appreciated!Code:Dim WC As New WebClient WC.Credentials = New NetworkCredential(username, password) WC.UploadFile("ftp://127.0.0.1/data/Models/", "C:\blender_cube.obj")




Reply With Quote
