Solved

FTP Hangs

Posted on 2002-05-23
10
589 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 70

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 70

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
 
LVL 70

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 70

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 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