无法使用vba从ftp获取文件

问题描述 投票:0回答:2

我想从 ftp 下载图像以便稍后在访问报告中使用它,问题是 ftpgetFile 返回 0 并且没有下载文件夹中的图像。

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

在 ftp 中,图像存在,它位于 /documents 中,名称类似于 123.jpg,我正在下载到我的下载文件夹中,并且不会出现错误。

提前谢谢您。

编辑

我尝试了你所说的,但它不起作用,并且 getLastError 返回 0

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

马丁给出的解决方案

确保标志值不是像本例一样为 0,而是 INTERNET_FLAG_PASSIVE

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

非常感谢大家的回答。

vba ms-access ftp wininet
2个回答
1
投票

lpszNewFile
的第三个
FtpGetFile
参数是文件路径,而不是目录。

所以应该是这样的:

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

此外,一般来说,您应该将

INTERNET_FLAG_PASSIVE
InternetConnect
一起使用。

如果没有该标志,

InternetConnect
默认为活动模式,当涉及防火墙或 NAT 时,该模式大部分不可用。


0
投票

我们成功地使用了这个变体:

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
© www.soinside.com 2019 - 2024. All rights reserved.