Archived Forum Post

Index of archived forum posts

Question:

Asynchronous FTP Upload VB.net work well in console project but very slow in web project

Jun 20 '14 at 12:29

Hi I'm trying to Upload files with Asynchronous FTP Upload it works well with Console Project vb.net

when i'm try to make the same code in a web aspx project it work but very slow and when i check the filezilla server i find the server blok in the reading files but when i kick the user the file complete the upload with success please any one has Idea :(

and this is my function:

    Public Sub FTpUpload(ByVal localname As String, ByVal remot As String, ByVal host As String, ByVal login As String, ByVal password As String)
        Dim ftp As New Chilkat.Ftp2()

        Dim success As Boolean
        System.Diagnostics.Debug.WriteLine("Start")

        '  Any string unlocks the component for the 1st 30-days.
        success = ftp.UnlockComponent("Anything for 30-day trial")
        If (success <> True) Then
            System.Diagnostics.Debug.WriteLine("tp.LastErrorText & vbCrLf1:" & ftp.LastErrorText & vbCrLf)
            Exit Sub
        End If

        ftp.Hostname = host
        ftp.Username = login
        ftp.Password = password

        '  Connect and login to the FTP server.

        ftp.Passive = True
        success = ftp.Connect()

        If (success <> True) Then
            System.Diagnostics.Debug.WriteLine("tp.LastErrorText & vbCrLf2:" & ftp.LastErrorText & vbCrLf)

            Exit Sub
        End If

        '  Change to the remote directory where the file will be uploaded.
        'success = ftp.ChangeRemoteDir("test")
        'If (success <> True) Then
        '    Console.WriteLine("tp.LastErrorText & vbCrLf3:" & ftp.LastErrorText & vbCrLf)

        '    Exit Sub
        'End If

        Dim localFilename As String
        localFilename = localname
        Dim remoteFilename As String
        remoteFilename = remot

        '  Begin the upload in a background thread.
        '  Only 1 background upload or download may be active at any time.
        '  (per instance of an FTP object)
        '  To append to an existing file in the background, call
        '  AsyncAppendFileStart instead.
        success = ftp.AsyncPutFileStart(localFilename, remoteFilename)
        If (success <> True) Then
            System.Diagnostics.Debug.WriteLine("tp.LastErrorText & vbCrLf:" & ftp.LastErrorText & vbCrLf)

            Exit Sub
        End If

        'The application is now free to do anything else
        'while the file is uploading.
        'For this example, we'll simply sleep and periodically
        'check to see if the transfer if finished.  While checking
        '      however, we 'll report on the progress in both number
        '  of bytes tranferred and performance in bytes/second.
        'While ftp.AsyncFinished <> True
        '    System.Diagnostics.Debug.WriteLine(ftp.AsyncBytesSent & " bytes sent" & vbCrLf)
        '    System.Diagnostics.Debug.WriteLine(" bytes per second" & vbCrLf)

        '    '  Sleep 1 second.
        '    ftp.SleepMs(1000)
        'End While

        '  Did the upload succeed?
        If (ftp.AsyncSuccess = True) Then
            System.Diagnostics.Debug.WriteLine("File Uploaded!" & vbCrLf)

        Else
            '  The error information for asynchronous ops
            '  is in AsyncLog as opposed to LastErrorText
            System.Diagnostics.Debug.WriteLine("ftp.AsyncLog & vbCrLf" & ftp.AsyncLog & vbCrLf)

        End If

        ftp.Disconnect()

        Console.WriteLine("END")

Answer

The code snippet you posted doesn't quite make sense for this reason: The call to ftp.AsyncPutFileStart starts a background thread to begin doing the FTP upload. The main thread of execution then continues and tries to call ftpDisconnect immediately, which makes no sense because the background thread that is doing the upload is surely not yet completed. In fact, you cannot initiate another FTP operation (where a command is sent to the FTP server) until the background upload is completed.