0

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.

3
  • What does GetLastError return after FtpGetFile call? Commented Feb 26, 2020 at 10:04
  • You miss the backslash: .. \Descargas\" & id, ... Commented Feb 26, 2020 at 11:51
  • Look at this post stackoverflow.com/questions/27455437/… Commented Feb 27, 2020 at 19:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Which of my two suggestions did help?
Both of them, but the cause of this problem was because de INTERNET_FLAG_ PASSIVE
0

We use this variation successfully:

Private Const FTP_TRANSFER_TYPE_UNKNOWN     As Long = 0
Private Const INTERNET_FLAG_RELOAD          As Long = &H80000000

<snip>

    If FtpGetFileA(hConn, strRemoteFile, strLocalFile, 1, 0, FTP_TRANSFER_TYPE_UNKNOWN Or INTERNET_FLAG_RELOAD, 0) Then
        Debug.Print "done"
    Else
        Debug.Print "fail"
    End If

2 Comments

Why do you think that INTERNET_FLAG_RELOAD will make any difference?
It works here and is different from what the questioneer uses - and is easy to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.