vbams-accessftpwininet

Cannot get files from ftp using vba


I want to download an image from ftp to use it later in a access report, the problem is the ftpgetFile returning 0 and not downloading the image in the folder.

Public Function RecuperarFirmaMuestreadores(userID As Long)
    Dim firma As String
    Dim id As String
    id = CStr(userID) & ".jpg"
    sign = ftpfirma("XXX.XX.XXX.XXX", "User", "password", "/document/", id)
End Function

Function ftpfirma(ByVal HostName As String, ByVal Username As String, ByVal Password As String, ByVal sDir As String, id As String) As String
    Dim sOrgPAth As String
    Dim pData As WIN32_FIND_DATA
    Dim hFind As Long, lRet As Long
    Dim hConnection, hOpen, hFile  As Long
    Dim sFiles() As String
    Dim firma As Long

    sPath = String(MAX_PATH, 0)

    hOpen = InternetOpen("FTPGET", 1, vbNullString, vbNullString, 1)
    hConn = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, Username, Password, 1, 0, 2)

    ' Change Directory
    Call FtpSetCurrentDirectory(hConn, sDir)

    ' get list of directory
    Call FtpGetCurrentDirectory(hConn, sPath, Len(sPath))

    Call FTPGetFile(hConn, id, "C:\Documents and Settings\Adrian\Mis documentos\Descargas", True, 0, 0 Or GENERIC_READ, 0)

    ' Close Internet Connection
    Call InternetCloseHandle(hOpen)
    Call InternetCloseHandle(hConn)
End Function

In the ftp the image exists and it's in /documents with a name like 123.jpg, and I'm downloading into my downloads folder and I don't get errors to work with.

Thank you in advance.

EDIT

I try what you say but it's not working, and getLastError return 0

Call FTPGetFile(hConn, id, "C:\Documents and Settings\Adrian\Mis documentos\Descargas\" & id, True, 0, INTERNET_FLAG_PASSIVE, 0)
e = getLastError()

SOLUTION GIVEN BY MARTIN

Make sure the flag value isn't 0 like this case and is INTERNET_FLAG_PASSIVE

 hConn = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, Username, Password, 1, INTERNET_FLAG_PASSIVE, 2)

Thank you so much to all for your answers.


Solution

  • The third lpszNewFile argument of FtpGetFile is a path to a file, not a directory.

    So it should be like:

    Call FTPGetFile(hConn, id, "C:\Documents and Settings\Adrian\Mis documentos\Descargas\" & id, ...)
    

    Also, in general, you should use INTERNET_FLAG_PASSIVE with InternetConnect.

    Without the flag, the InternetConnect defaults to the active mode, which is mostly unusable, when a firewall or a NAT is involved.