Public Const BUFFERSIZE = 255
Public Const INTERNET_FLAG_PASSIVE = &H8000000
Public Const FTP_TRANSFER_TYPE_BINARY = &H2
Public Const FTP_TRANSFER_TYPE_ASCII = &H1
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
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 lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public 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
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Long
Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Public 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
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToRead As Long, dwNumberOfBytesRead As Long) As Integer
Public Declare Function InternetWriteFile Lib "wininet.dll" (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToWrite As Long, dwNumberOfBytesWritten As Long) As Integer
Public hOpen As Long
Public hConnection As Long
Public hFile As Long
Public dwType As Long
Public dwSeman As Long
' FTP서버 접속
Public Function FtpOpen(strIP As String, strPort As String, strUser As String, strPassword As String) As Boolean
'기존에 이미 접속되어 있으면 기존 접속 종료
If hConnection <> 0 Then InternetCloseHandle hConnection
If (InternetWriteFile(hFile, Data(0), BUFFERSIZE, Written) = 0) Then Exit Function
DoEvents
Sum = Sum + BUFFERSIZE
objTarget.PBar.Value = Sum
DoEvents
Next
Get #1, , Data
If (InternetWriteFile(hFile, Data(0), lonSize Mod BUFFERSIZE, Written) = 0) Then Exit Function
Sum = Sum + (lonSize Mod BUFFERSIZE)
lonSize = Sum
Close #1
InternetCloseHandle (hFile)
FTPUpload = True
End If
End Function
4. 접속 종료
'FTP접속종료
Public Sub FtpClose()
If hConnection <> 0 Then InternetCloseHandle hConnection
hConnection = 0
End Sub
5. 기능 사용예
If FtpOpen(FTP 접속 IP, FTP 접속 Port, FTP 접속 사용자 이름, FTP 접속 비밀번호) = False Then
MsgBox "FTP 서버접속 실패!", vbCritical, "확인"
Exit Sub
End If
If FTPUpload(업로드할 로컬 파일, 원격 서버 파일, ProgrssBar를 가지고 있는 폼 객체) = False Then
MsgBox "FTP 서버전송 실패!", vbCritical, "확인"
End If
Call FtpClose
MsgBox "전송 완료"
Unload frmProgress
※ 테스트시 유의할점
테스트를 위해서 기본 폼 외에 ProgressBar를 가지고 있는 폼을 따로 생성해두어야 합니다.
따로 폼 생성을 하기 싫으면 함수 모듈 중 FTPUoload 함수 마지막 인자를 없애고
FTPUpload 함수 내 objTarget 객체를 삭제를 하여 사용을 하시기 바랍니다.