Ответить на сообщение
Вернуться к теме
Вы отвечаете на сообщение:
ник: Силыч объявляете класс 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")
Ваше имя:
Пароль:
Сообщение:
Прикрепить:
Для вставки смайлов в текст щелкните по значку.