tempbuf = bytes()
'If the URL returned any thing, put the first
'50 characters in a buffer, Error messages
'will be found here.
If Len(strResult) > 50 Then
buf = Left(strResult, 100)
Else
buf = bytes()
End If
'Catch a time out error
If Err = 35761 Then
tout = tout + 1
Status = "Timed out: " & tout
Err.Clear
Exit Function
'If nothing is returned, it usually means
'that the server was not found.
ElseIf tempbuf = "" Then
snf = snf + 1
Status = "Server not found: " & snf
Exit Function
'if nothing is returned, it usually means that
'the server was found, but the requested file was
'not present.
ElseIf InStr(1, buf, "404") Then
fnf = fnf + 1
Status = "File not found: " & fnf
Exit Function
Else
'Otherwise, everything is OK
OK = OK + 1
Status = "File was downloaded successfully: "
& OK
End If
If the downloaded string is larger than 50 chars then transfer the first 100 characters to the buffer so that the error messages can be searched for. 35761 is the time out error.
'Get a file number
fn = FreeFile
'Open a binary file and load data into it!
Open sOutPutFile For Binary Access Write As #fn
Put #fn, , bytes()
DoEvents
'Close the open file
Close #fn
Now its time to write the information retrieved from the remote server to the local file. We have used FreeFile function here, FreeFile function returns an integer representing the next file number available for use by the open statement.1-255 file numbers can be used to open files not accessible to other applications. Use file numbers in the range 256-511 for files accessible from other applications. SOutPutFile is the name of the output file provided by the user. We have opened the file for binary access. Put the downloaded bytes into the newly opened file. After writing the content to the file, close the file. That's it. I used this component to download image files from remote computers and it always worked fine.
Private Sub Class_Terminate()
Set Form1 = Nothing
End Sub
In the class_terminate() event of the class, destroy the form on which we placed the control.
Comments