Using the Inet Control

Saving the file

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.

You might also like...

Comments

About the author

S.S. Ahmed United States

S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski