Uploading files to web using ftp using VB.NET from desktop application

fmichail
fmichail used Ask the Experts™
on
I am trying to upload group of jpg files using vb.net
         dim thename as string="myfile.jpg"
         dim filename as string="c:\myfolder\myfile.jpg"
         'create request
         Dim mReq As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://subdomain.domain.com/client_photos/" & thename), System.Net.FtpWebRequest)

                        With mReq
                            .Credentials = New System.Net.NetworkCredential(userid, Password)
                            .Method = System.Net.WebRequestMethods.Ftp.UploadFile
                        End With

                        'Read a file
                        Dim mFile() As Byte
                        mFile = System.IO.File.ReadAllBytes(fileName)

                        'Upload
                        Dim mStream As System.IO.Stream = mReq.GetRequestStream()
                        mStream.Write(mFile, 0, mFile.Length)

                        'Cleanup
                        mStream.Close()
                        mStream.Dispose()

An exception is raised in executing GetRequestStream() and the error is that file not found... It means that the  mreq could not create the file on the remote server.... I copied this code from the net, and nobody seems to have a problem... I must be doing something wrong... Help please. Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
I think this is a better implementation
https://www.codeexplained.net/uploadftpfilevbnet/

Imports System.IO
Imports System.Net



Public Class RadForm1

    Dim filename As String
    Dim filename As String
    'create request
    Dim strFTPAddress As String
    Dim strftpuser As String
    Dim strftpassword As String
    Dim strtargetfilename As String
    Dim boldeleteafterupload As Boolean

    Public Function UploadFTPFiles(ftpAddress As String, ftpUser As String, ftpPassword As String,
                               fileToUpload As String, targetFileName As String,
                               deleteAfterUpload As Boolean,
                               ExceptionInfo As Exception) As Boolean

        Dim credential As NetworkCredential

        Try
            credential = New NetworkCredential(ftpUser, ftpPassword)

            If ftpAddress.EndsWith("/") = False Then ftpAddress = ftpAddress & "/"

            Dim sFtpFile As String = ftpAddress & fileToUpload

            Dim request As FtpWebRequest = DirectCast(WebRequest.Create(sFtpFile), FtpWebRequest)

            request.KeepAlive = False
            request.Method = WebRequestMethods.Ftp.UploadFile
            request.Credentials = credential
            request.UsePassive = False
            request.Timeout = (60 * 1000) * 3 '3 mins

            Using reader As New FileStream(fileToUpload, FileMode.Open)

                Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte
                reader.Read(buffer, 0, buffer.Length)
                reader.Close()

                request.ContentLength = buffer.Length
                Dim stream As Stream = request.GetRequestStream
                stream.Write(buffer, 0, buffer.Length)
                stream.Close()

                Using response As FtpWebResponse = DirectCast(request.GetResponse, FtpWebResponse)

                    If deleteAfterUpload Then
                        My.Computer.FileSystem.DeleteFile(fileToUpload)
                    End If

                    response.Close()
                End Using

            End Using

            Return True

        Catch ex As Exception
            ExceptionInfo = ex
            Return False
        Finally

        End Try

    End Function
End Class

Open in new window

Commented:
With most FTP routines I have to program, I usually use 3rd party solutions (WinSCP is totally free!), as I see this as "re-inventing the wheel".
Think of it, step into the FTPS or SFTP realm, and you'll have another headache to worry about. Using WinSCP? Just add one or two extra lines of code.
Error handling, same thing. Already done, no need to think about. And the list goes on and on and on.

Anyway, for WinSCP, just point the code to the DLL and read up on this: https://winscp.net/eng/docs/library

Author

Commented:
Dear David,
I applied your code, and passed the following parameters:


I got the same error as in the attached screen shot.
It is the same error that I had before...

What does it mean file does not exist ??

What Am I doing wrong... Please help
uploading-Error.jpg

Author

Commented:
Dear David,
I found the reason, and used your code (It is very organized code). However, I added and edited little part of the code

        'Added By Fayez (To pick up the stripped file name from the path
        Dim thename As String = fileToUpload.Substring(fileToUpload.LastIndexOf("\") + 1)

        'Changed By Fayez to use the stripped name
        'Dim sFtpFile As String = ftpAddress & fileToUpload
        Dim sFtpFile As String = ftpAddress & thename

The reason was that the ftp address should have been ftp://dareleman.suredatasolutions.ca/wwwroot/client_photos instead of ftp://dareleman.suredatasolutions.ca/client_photos.

Thanks for the great solution

Author

Commented:
Dear David,
I found the reason, and used your code (It is very organized code). However, I added and edited little part of the code

        'Added By Fayez (To pick up the stripped file name from the path
        Dim thename As String = fileToUpload.Substring(fileToUpload.LastIndexOf("\") + 1)

        'Changed By Fayez to use the stripped name
        'Dim sFtpFile As String = ftpAddress & fileToUpload
        Dim sFtpFile As String = ftpAddress & thename

The reason was that the ftp address should have been ftp://dareleman.suredatasolutions.ca/wwwroot/client_photos instead of ftp://dareleman.suredatasolutions.ca/client_photos.

Thanks for the great solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial