Solved

FTP Hangs

Posted on 2002-05-23
10
588 Views
Last Modified: 2008-02-07
I am using a method to ftp files, it works very well when uploading one or two files, but when i upload multiple files in a loop at about the 3rd or 4th file it often hangs and i have to end task.
What could i be doing wrong?
Is there a better method i could consider?

Regards


Here is the FTP Code

Public Function MMputFtpFile(hostname As String, UserName As String, UserPassword As String, HostFilename As String, localFilename As String, Optional TransferMode As eFtpTransferType = FTP_TRANSFER_TYPE_ASCII) As Long
    Dim hInternet As Long
    On Error GoTo e
    Dim hFTP As Long
    Dim sDir As String
    Dim l As Long
    hInternet = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
    hFTP = InternetConnect(hInternet, hostname, INTERNET_DEFAULT_FTP_PORT, UserName, UserPassword, INTERNET_SERVICE_FTP, 0, 0)
    If hFTP <> 0 Then
        Debug.Print HostFilename
        Debug.Print localFilename
        l = InStrRev(HostFilename, "/")
        sDir = Left(HostFilename, l)
        FtpCreateDirectory hFTP, "/FastTrackTeam"
        FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads"
        FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads/" & MyUser.UserID
        FtpCreateDirectory hFTP, sDir
        MMputFtpFile = FtpPutFile(hFTP, localFilename, HostFilename, INTERNET_FLAG_DONT_CACHE + TransferMode, 0)
        DoEvents
        Call InternetCloseHandle(hInternet)
        DoEvents
    Else
        MMputFtpFile = 0
    End If
Exit Function
e:
    HandleError Err, "MMPutFTPFILE"
End Function


-----
Here is the loop


For Each mes In m_colMessages
       
        lTemp = 0
        For Each f In mes.Files
            lTemp = lTemp + 1
            lblStatus.Caption = "Uploading " & lTemp & " Of " & mes.Files.Count
            DoEvents
            MMputFtpFile g_FTP_SERVER, g_FTP_USERID, g_FTP_PASS, f.RemotePath, f.Path
            DoEvents
        Next f
       Next mes
0
Comment
Question by:crazyman
10 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 7029483
Do you know where it hangs
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7029495
I had a problem like this that I solved by closing the connection and re-opening it.
0
 
LVL 13

Author Comment

by:crazyman
ID: 7029585
Hangs on the actual FtpPutFile line
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7029634
have you tried closing and re-opening the connection?
0
 
LVL 13

Author Comment

by:crazyman
ID: 7029649
according to AllApi.net
calling
Call InternetCloseHandle(hInternet)
will cause any lower level connections to be dropped.
so i do open and close the connection each time...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 69

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 7029686
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7029780
This is probably unrelated, but you should also explicitly close the handle to the FTP server (hFTP).  In addition you are only closing the handle connection to the Internet (hInternet) when you successfully open a handle to the FTP server.  So perhaps your code should be:

hInternet = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
If hInternet Then
   hFTP = InternetConnect(hInternet, hostname, INTERNET_DEFAULT_FTP_PORT, UserName, UserPassword, INTERNET_SERVICE_FTP, 0, 0)
   If hFTP <> 0 Then
      Debug.Print HostFilename
      Debug.Print localFilename
      l = InStrRev(HostFilename, "/")
      sDir = Left(HostFilename, l)
      FtpCreateDirectory hFTP, "/FastTrackTeam"
      FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads"
      FtpCreateDirectory hFTP, "/FastTrackTeam/Uploads/" & MyUser.UserID
      FtpCreateDirectory hFTP, sDir
      MMputFtpFile = FtpPutFile(hFTP, localFilename, HostFilename, INTERNET_FLAG_DONT_CACHE + TransferMode, 0)
      DoEvents
      InternetCloseHandle hFTP
      DoEvents
   Else
      MMputFtpFile = 0
   End If
   InternetCloseHandle hInternet
Else
   ' Could not get a handle to the internet
End If

Again, probably totally unrelated.

Anthony
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7030083
Crazy, take in mind what emoreau's saids: Close and open the connection.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7030147
Richie_Simonetti, I had overlooked (and you may have too) the code. If you look carefully, you will find that the connection is already open and closed.
0
 
LVL 13

Author Comment

by:crazyman
ID: 7031413
Well i have changed the code to use the class that from freevbcode, this seems to work much better and of course i now have the bonus of a progress bar.


Thanks to all who commented on this question, points go to emoreau.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Use closed file on desktop in vba 6 67
Windows Script Host failed (Access is denied.) error 6 109
Spell Check in VB6 13 99
fso.FolderExists("\\server\HiddenFolder$") 4 52
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now