FTPGet
G'day,
I want to download a file from an FTP site, programmatically.
The code included runs in MSAccess 2002 running under Windows XP pro.
The same sode compiles under Visual Studio 2003 but always returns False from the FtpGet() function. I have installed Service Pack 1.
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, _
ByVal nServerPort As Integer, ByVal sUsername As String, _
ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" ( _
ByVal hFtpSession As Long, _
ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, _
ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Sub btnFTP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFTP.Click
Const ftpServerName = "Enter your server address"
Const UserName = "Enter your Username"
Const Password = "Enter your password"
Const FTP_PATH = "Enter the path on your FTP server" ' No leading '/'
const FTP_FILE = FTP_PATH & "FTPfile.txt"
Const LOCAL_PATH = "Enter your local path to receive the downloaded file"
const LOCAL_FILE = LOCAL_PATH & "LocalFile.txt"
Dim lngINet As Long, lngINetConn As Long, blnRC As Boolean
'Open an Internet session without using a proxy
lngINet = InternetOpen("SessionName", 1, vbNullString, vbNullString, 0)
'Connect to the host
lngINetConn = InternetConnect(lngINet, ftpServerName, 21, UserName, Password, 1, 0, 0) 'OK
'In case the local file already exists, weâll overwrite it.
blnRC = FtpGetFile(lngINetConn, RemoteFile, LocalFile, 0, 0, 1, 0)
If blnRC Then
' I ALWAYS end up here!?
' Indicates that the download failed, which it did!
MessageBox.Show("DOWNLOAD was successfull!")
Else
MessageBox.Show("Download FAILED!")
End If
' Close handles
InternetCloseHandle(lngINetConn)
InternetCloseHandle(lngINet)
End Sub
I must have an environment problem with Visual Sudio! Please help!
|