Results 1 to 4 of 4

Thread: [RESOLVED] FTP Upload error " Invalid URI: The format of the URI could not be determined. "

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Resolved [RESOLVED] FTP Upload error " Invalid URI: The format of the URI could not be determined. "

    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:

    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
    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:
    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
    I get the error "Invalid URI: The format of the URI could not be determined." on the first line of the function:

    Code:
    Dim ftpRequest As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri(host + "/" + Path.GetFileName(folderToUploadTo))), FtpWebRequest)
    Can someone please help me, I've spent days looking at different ways and examples of how to do it from things like:

    Code:
    My.Computer.Network.UploadFile("C:\blender_cube.obj", "ftp://127.0.0.1/data/", username, _
                                          password, True, 100)
    TO
    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")
    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!

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: FTP Upload error " Invalid URI: The format of the URI could not be determined. "

    Have you tried printing the address to a message box to see if it actually looks the way you want it to?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: FTP Upload error " Invalid URI: The format of the URI could not be determined. "

    Yeah, it comes out as " 127.0.0.1/data " which is the first shared directory. I've gone through all my settings on fillezilla and my permissions on my os (to allow the user to have writes to do everything, write, read delete and create)

    EDIT: Oh wow, can't believe I've already made a silly mistake. I've change the root parameter to be " ftp://127.0.0.1 " instead as you have to specify the the protocol you are using first to make the URI valid (I'm being a moron!). However the next problem I run into is " The remote server returned an error: (550) File unavailable (e.g., file not found, no access). " on this line of code:

    Code:
      Dim requestStream As Stream = ftpRequest.GetRequestStream()

    EDIT 2:

    Ok, I've gotten it to work, you have to include the filename (e.g.blender_cube.obj) at the end of the file, I'll post the function and an example below.
    Last edited by VintageCoro; Apr 4th, 2013 at 08:09 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Post Re: FTP Upload error " Invalid URI: The format of the URI could not be determined. "

    Here is a function that works correctly for uploading a file via the FTP Protocol in VB.net using the system.net approach:

    First here's the function: (I recommend placing it inside of a module)
    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, holds the value of the path you are trying to reach
                Dim ftpRequest As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri(host & "/" & Path.GetFileName(folderToUploadTo))), FtpWebRequest)
            
                'pass in our login credentials
                ftpRequest.Credentials = New NetworkCredential(username, password)
    
                'set the method to Upload, since we're uploading a file
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
    
    
                '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
    Notes:
    "f" = The path and filename of the file you want to transfer (e.g. C:\blender_cube.obj) It's important you follow this syntax correctly, you have to firstly have the drive location (in this case "C:") followed by a "/", followed by the file name "blender_cube" and finally the file type of the file .obj)

    "host" = The location of the server you want to connect to, this can be done through either connecting to the domain or IP Address (e.g. Domain: ftp://myftpexample.com or IP Address: ftp://127.0.0.1) Make sure you include ftp://.

    "username" = This is the username that you have created on the FTP server. (In my case, I'm using FileZilla as my FTP management application, on there I can create different usernames and give different permissions to those users. An example of a username would be: Johndoe)

    "password" = This is the password that you have assigned to the username (An example of a password would be ILoveVBWhenIMakeItWork2013)

    "folderToUploadTo" = This is the folder location on the server to where you want to save the file, below you will see how these parameters should be passed into this function (An example of this is: data/blender_cube.obj)


    Here's how to call the function: (this example is via using a button on a form)
    Code:
    Private Sub btnUploadFile_Click(sender As System.Object, e As System.EventArgs) Handles btnUploadFile.Click
            UploadFileToFtp("C:\blender_cube.obj", "ftp://127.0.0.1", "Johndoe", "ILoveVBWhenIMakeItWork2013", "data/blender_cube.obj")
    End Sub
    I hope this helps anyone running into problems with FTP on vb.net. Remember to check your sharing properties of the folder (on windows, sever side) and the settings on your FTP management application.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width