vbdev04
asked on
Binary File Download using InternetReadFile
How do I download binary file using InternetReadFile in Visual Basic 6?
Public Declare Function InternetReadFile Lib "wininet.dll" _
(ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _
lNumberOfBytesRead As Long) As Integer
As sBuffer is declared as String, it returns String.
Public Declare Function InternetReadFile Lib "wininet.dll" _
(ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _
lNumberOfBytesRead As Long) As Integer
As sBuffer is declared as String, it returns String.
ASKER
Thanks but I want to use HTTP protocol instead of FTP.
I am sorry I overlooked that requirement in your question. Hopefully someone else can step in.
ASKER
Actually it is not your fault as I should have specified that in my question.
Does your function require anonymous FTP? also what are valid values for lTransferType?
Thanks,
>>Does your function require anonymous FTP?<<
Nope.
>>also what are valid values for lTransferType?<<
Public Enum TransferTypesEnum
ASCII = FTP_TRANSFER_TYPE_ASCII
Binary = FTP_TRANSFER_TYPE_BINARY
End Enum
Nope.
>>also what are valid values for lTransferType?<<
Public Enum TransferTypesEnum
ASCII = FTP_TRANSFER_TYPE_ASCII
Binary = FTP_TRANSFER_TYPE_BINARY
End Enum
I should have stated that hConnect is a private member of the class and it assumes it has already been initialized as in:
hInternet = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRI TE)
hConnect = InternetConnect(hInternet, sHost, mvarPort, sUsername, sPassword, INTERNET_SERVICE_FTP, dwFlags, &H0)
Just make sure you close them at the end, as in:
Call InternetCloseHandle(hConne ct)
Call InternetCloseHandle(hInter net)
hInternet = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_DIRECT,
hConnect = InternetConnect(hInternet,
Just make sure you close them at the end, as in:
Call InternetCloseHandle(hConne
Call InternetCloseHandle(hInter
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>I got it to work using code from ...<<
Great, I am gald to hear it. Please close this questiom.
Great, I am gald to hear it. Please close this questiom.
Fine with me.
Private Sub GetLongFile(ByVal sSrcFilename As String, ByVal sDstFilename As String, ByVal lTransferType As Long, ByVal FileSize As Currency)
Const BufferLen = 2048
Dim hFtpFile As Long
Dim hLocalFile As Long
Dim Buffer As String
Dim BytesRead As Long
Dim BytesWritten As Long
Dim BytesTransferred As Currency
Dim CancelCopy As Boolean
CancelCopy = False
hFtpFile = FtpOpenFile(hConnect, sSrcFilename, GENERIC_READ, lTransferType Or INTERNET_FLAG_RELOAD, 0&)
If hFtpFile Then
hLocalFile = CreateFile(sDstFilename, GENERIC_WRITE, 0&, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&)
If hLocalFile Then
Buffer = Space$(BufferLen)
BytesRead = 0
BytesTransferred = 0
CancelCopy = False
Do
If InternetReadFile(hFtpFile,
' Due to a bug with the InternetReadFile API,
' we have to read the whole file before closing!
' Otherwise it appears to hang forever when hFtpFile is closed
If Not CancelCopy Then
If WriteFile(hLocalFile, Buffer, BytesRead, BytesWritten, 0&) Then
BytesTransferred = BytesTransferred + BytesWritten
RaiseEvent Progress(BytesTransferred,
Else
Err.Raise Err.LastDllError
CancelCopy = True 'So the file gets deleted and exits the loop
End If
End If
Else
Err.Raise Err.LastDllError
CancelCopy = True 'So the file gets deleted
End If
DoEvents
Loop While BytesRead > 0 'And Not CancelCopy
CloseHandle hLocalFile
If CancelCopy Then
DeleteFile sDstFilename
End If
Else
Err.Raise Err.LastDllError
End If
InternetCloseHandle hFtpFile
hFtpFile = 0
Else
Err.Raise Err.LastDllError
End If
End Sub