Solved

FTP Hangs

Posted on 2002-05-23
10
587 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
Comment Utility
Do you know where it hangs
0
 
LVL 69

Expert Comment

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

Author Comment

by:crazyman
Comment Utility
Hangs on the actual FtpPutFile line
0
 
LVL 69

Expert Comment

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

Author Comment

by:crazyman
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 69

Accepted Solution

by:
Éric Moreau earned 100 total points
Comment Utility
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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
Comment Utility
Crazy, take in mind what emoreau's saids: Close and open the connection.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now