Доброго времени суток, Посетитель!
|
|
|
|
|
|
|
|
|
вид форума:
|
|
|
|
| Добрый день.
Задача скопировать файл с ftp на диск с помощью VB Access 2003
нашел на этом сайте http://forum.codenet.ru/showthread.php?t=32353 пример переноса инф с ftp в папку на диске, делаю так:
Public 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
Public Declare Function InternetOpen _
Lib "wininet.dll" Alias "InternetOpenA" ( _
ByVal sAgent As String, _
ByVal nAccessType As Long, _
ByVal sProxyName As String, _
ByVal sProxyBypass As String, _
ByVal nFlags As Long) As Long
Public 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 nService As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long
Public Declare Function InternetCloseHandle _
Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Const INTERNET_SERVICE_FTP = 1
Public Const INTERNET_SERVICE_GOPHER = 2
Public Const INTERNET_SERVICE_HTTP = 3
' Далее по кнопарю на форме:
hINetSession = InternetOpen("IExplorer", 0, vbNullString, vbNullString, 0)
hSession = InternetConnect(hINetSession, "10.100.1.139", 0, "yyyyyy", "332211", INTERNET_SERVICE_FTP, 0, 0)
If FtpGetFile(hSession, "iq/Адреса.xls", "E:\iq\FTP\1\Адреса.xls", False, 0, 1, 0) = False Then
MsgBox "не удалось"
Else
MsgBox "работа выполнена"
End If
Call InternetCloseHandle(hSession)
Call InternetCloseHandle(hINetSession)
|
Переменная hSession=0, переменная hINetSession принимает не нулевое значение.... скачать файл по ftp не получается
помогите разобраться, что сделано не так | |
|
| |
|
|
|
| Вы хотите неприменно wininet.dll использовать?
Internet Transfer ActiveX Control - не пойдет?
http://support.microsoft.com/kb/163653 | |
|
| |
|
|
|
| да мне в общем то не принципиально, что использовать для выполнения этой задачи, лишь бы работало)))
сейчас попробую предложенный вами вариант...
а с моим кодом в чем проблема, почему не работает ? есть версии? | |
|
| |
|
|
|
| объявляете класс FTP
Option Compare Database
Option Explicit
'-- Set Constants
Private Const MAX_PATH = 260
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const NO_ERROR = 0
Private Const ERROR_NO_MORE_FILES = 18
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Const FILE_ATTRIBUTE_COMPRESSED = &H800
Private Const FILE_ATTRIBUTE_OFFLINE = &H1000
Private Const INTERNET_FLAG_PASSIVE = &H8000000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_INVALID_PORT_NUMBER = 0
Private Const INTERNET_SERVICE_FTP = 1
Private Const FTP_TRANSFER_TYPE_BINARY = &H2
Private Const FTP_TRANSFER_TYPE_ASCII = &H1
Private Const INTERNET_DEFAULT_FTP_PORT = 21
Private Const GENERIC_WRITE = &H40000000
Private Const BUFFER_SIZE = 100
Private Const PassiveConnection As Boolean = True
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As Currency
ftLastAccessTime As Currency
ftLastWriteTime As Currency
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
'-- Declare wininet.dll API Functions
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _
(ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
(ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As Any, lpLocalFileTime As Any) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" _
(ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Boolean
Private Declare Function InternetWriteFile Lib "wininet.dll" _
(ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToWite As Long, _
dwNumberOfBytesWritten As Long) As Integer
Private Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" _
(ByVal hFtpSession As Long, ByVal sBuff As String, ByVal Access As Long, ByVal flags As Long, ByVal Context As Long) As Long
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" _
Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
ByVal lpszFileName As String) As Boolean
Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Long
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 InternetGetLastResponseInfo Lib "wininet.dll" _
Alias "InternetGetLastResponseInfoA" _
(ByRef lpdwError As Long, _
ByVal lpszErrorBuffer As String, _
ByRef lpdwErrorBufferLength As Long) As Boolean
'-- define variables
Public bInitialize As Boolean
Public CurRemoteDir$
Private hOpen&
Private hFile&
Private hConnection&
Private ok%
Private dwType&
Private Sub Class_Initialize()
Dim UserName$, Password$, HostName$
bInitialize = False
UserName = "<заполнить>"
Password = "<заполнить>"
HostName = "<заполнить>"
'-- Open Internet Connecion
hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
'-- Connect to FTP
hConnection = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, _
INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
If (hConnection <> 0) Then
bInitialize = True
Else
ShowError
End If
End Sub
Private Sub Class_Terminate()
If hFile <> 0 Then Call InternetCloseHandle(hFile)
If hConnection <> 0 Then Call InternetCloseHandle(hConnection)
If hOpen <> 0 Then Call InternetCloseHandle(hOpen)
bInitialize = False
End Sub
Public Sub ShowError()
Dim lErr&, sErr$, lenBuf&
'-- get the required buffer size
InternetGetLastResponseInfo lErr, sErr, lenBuf
'-- create a buffer
sErr = String(lenBuf, 0)
'-- retrieve the last respons info
InternetGetLastResponseInfo lErr, sErr, lenBuf
'-- show the last response info
MsgBox "Last Server Response : " + sErr, vbOKOnly + vbCritical
End Sub
Public Property Get RemoteSizeOf&(FRemote$)
Dim hFind&
Dim nLastError&
Dim dError&
Dim pData As WIN32_FIND_DATA
RemoteSizeOf = 0
If (bInitialize = 0) Then Exit Property
hFind = FtpFindFirstFile(hConnection, FRemote, pData, 0, 0)
nLastError = Err.LastDllError
If hFind = 0 Then
If (nLastError = ERROR_NO_MORE_FILES) Then
MsgBox "This directory is empty!", , "FTP"
Else
MsgBox Err.LastDllError, "FtpFindFirstFile"
End If
Exit Property
End If
dError = NO_ERROR
RemoteSizeOf = pData.nFileSizeLow
End Property
'-- метод GetRemoteDir
Public Property Get GetRemoteDir$()
If (bInitialize = 0) Then Exit Property
CurRemoteDir = String(1024, Chr$(0))
If (FtpGetCurrentDirectory(hConnection, CurRemoteDir, 1024) = False) Then
ShowError
GetRemoteDir = &H0
Else
GetRemoteDir = CurRemoteDir
End If
End Property
'-- метод SetRemoteDir
Public Sub SetRemoteDir(RemoteDir$)
If (bInitialize = 0) Then Exit Sub
If (FtpSetCurrentDirectory(hConnection, RemoteDir) = False) Then
ShowError
End If
End Sub
'-- метод load файла c ftp
'-- 0- ok; 1- failure; 2- CRC filesize failure
Public Function GetRemoteFile%(FRemote$, FLocal$)
Dim fso As Object
Dim f As Object
Dim rSize&, lSize&
GetRemoteFile = 0
If (bInitialize = 0) Then Exit Function
If (FtpGetFile(hConnection, FRemote, FLocal, False, _
FILE_ATTRIBUTE_NORMAL, dwType Or INTERNET_FLAG_RELOAD, 0) = False) Then
ShowError
GetRemoteFile = 1
Else
'-- CRC filesize
rSize = RemoteSizeOf(FRemote)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(FLocal)
lSize = f.size
If rSize <> lSize Then
GetRemoteFile = 2
Exit Function
End If
End If
End Function
'-- метод upload файла на ftp
Public Function PutRemoteFile&(FLocal$, FRemote$)
Dim fso As Object
Dim f As Object
Dim rSize&, lSize&
PutRemoteFile = 0
If (bInitialize = 0) Then Exit Function
If (FtpPutFile(hConnection, FLocal, FRemote, dwType, 0) = False) Then
ShowError
PutRemoteFile = 1
Else
'-- CRC filesize
rSize = RemoteSizeOf(FRemote)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(FLocal)
lSize = f.size
If rSize <> lSize Then
PutRemoteFile = 2
Exit Function
End If
End If
End Function
|
использовать примерно так
Set ftps = New FTP
Debug.Print ftps.GetRemoteFile("/agent00/from1c.dat", "D:\tmp\xxxx.dat")
Debug.Print ftps.PutRemoteFile("D:\tmp\shema2.gif", "/agent01/shema2.gif")
Debug.Print ftps.SetRemoteDir "/agent00/"
Debug.Print ftps.GetRemoteDir
Debug.Print ftps.SizeOf("/agent00/from1c.dat")
|
эххх, нарытое затак отдаю | |
|
| |
|
|
|
| вот это вам спасибо огромное | |
|
| |
|
|
|
| чет я не нашел Internet Transfer ActiveX Control , есть такое Microsoft Internet Controls | |
|
| |
|
|
|
| это файл MSINET.OCX
http://support.microsoft.com/kb/179117
http://support.microsoft.com/kb/297391
http://support.microsoft.com/kb/873254
взать можно здесь
http://www.ocxdump.com/download-ocx-files_new.php/ocxfiles/M/MSINET.OCX/6.01.9782/download.html
Почему не работает ВАш код, надо разбираться.... | |
|
| |
|
|
|
| Спасибо Osmor, я разобрался, все работает, ты помог. Respect | |
|
| |
HiProg.com - Технологии программирования
|