Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

FTP Hangs

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
crazyman
Asked:
crazyman
1 Solution
 
EDDYKTCommented:
Do you know where it hangs
0
 
Éric MoreauSenior .Net ConsultantCommented:
I had a problem like this that I solved by closing the connection and re-opening it.
0
 
crazymanAuthor Commented:
Hangs on the actual FtpPutFile line
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Éric MoreauSenior .Net ConsultantCommented:
have you tried closing and re-opening the connection?
0
 
crazymanAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
Anthony PerkinsCommented:
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
 
Richie_SimonettiCommented:
Crazy, take in mind what emoreau's saids: Close and open the connection.
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
crazymanAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now