Hi. I'm fairly new to VB. I've made a handful of small programs and followed a few tutorials, but this is really the most complex program I've made so far. (Just a heads up that I may be missing something fairly obvious :$)

I'm working on a VB program which shrinks an image and sends it to a server over FTP. I found the following code snippet on another forum a week or two back (I can't recall exactly where), but I'm having a bit of an issue with it.

Private Sub FTP()

        'Variables
        Dim cls_request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(My.Settings.url), System.Net.FtpWebRequest)

        'Establish credentials for logging into ftp site
        cls_request.Credentials = New System.Net.NetworkCredential(My.Settings.username, My.Settings.password)

        'Set properties
        cls_request.KeepAlive = False
        cls_request.Proxy = Nothing
        cls_request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        cls_request.UseBinary = True

        'Read in the file
        Dim b_file() As Byte = System.IO.File.ReadAllBytes(OpenFileDialog1.FileName)

        'Upload the file
!      cls_request.GetRequestStream.Write(b_file, 0, b_file.Length)
        cls_request.GetRequestStream.Close()
        cls_request.GetRequestStream.Dispose()
        MessageBox.Show("Success")
    End Sub

When I run it, I get "A first chance exception of type 'System.Net.WebException' occurred in System.dll" and an error stating "The requested URI is invalid for this FTP command" in the code window. The program itself freezes. (I marked the line with an !)

From what I can tell, the issue seems to be that there is no filename specified for the upload, but I'm not sure how I would add that. (or even if that's the problem :confused:)

I've worked at this issue for a few hours and haven't made any progress, so I'm hoping someone will be able to help me solve this annoying error.

Thanks!

I seem to have found a better way to upload using My.Computer.Network.UploadFile

Unfortunately, the debugger now complains that I haven't specified a filename.

My code is now

Private Sub Upload()
        My.Computer.Network.UploadFile(OpenFileDialog1.FileName, My.Settings.url & My.Settings.remotefolder & OpenFolderDialog1.SafeFileName, My.Settings.username, My.Settings.password)
End Sub

The issue here is that I'm not using a string for the URL. I have successfully uploaded a file using a string (ftp://host/picture.jpg).

Does anybody know how I can use these variables as the url?

It seems I've answered my own question again. After some digging I found out that you declare a URI, so once I did that, It worked perfectly.

Here's the code in case anyone's interested.

Dim uriWebSite As New Uri(My.Settings.url & My.Settings.remotefolder & OpenFileDialog1.SafeFileName)
My.Computer.Network.UploadFile(OpenFileDialog1.FileName, uriWebSite, My.Settings.username, My.Settings.password)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.