HOWTO: Download a Binary File with the Internet Transfer Control via the GetChunk Method

ID: Q229666


The information in this article applies to:


SUMMARY

When using the GetChunk method of the Internet Transfer control to download binary files, the downloaded file may be larger than the original file. This is the case if the Variant returned from the GetChunk method is written directly to disk.


MORE INFORMATION

A Variant containing an array requires 12 bytes more than the array alone. This extra data is used to describe the contents of the variant. If the variant is written directly to disk, the 12 extra bytes are written to disk as well as the array. To avoid this situation, assign the variant to an intermediate byte array and write that array to disk. The following code illustrates this method:


Private Sub Command1_Click()
    Inet1.AccessType = icUseDefault

    Inet1.URL = "http://example.microsoft.com/somefile.exe"
    Inet1.Execute , "GET"
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
    Dim vtData As Variant
    
    Select Case State
    ' ... Other cases not shown.
  
    Case icResponseCompleted  ' 12
        Dim bDone As Boolean: bDone = False
        Dim tempArray() As Byte
        
        filesize = Inet1.GetHeader("Content-length")
        contenttype = Inet1.GetHeader("Content-type")

        Open "C:\somefile.exe" For Binary Access Write As #1
    
        ' Get first chunk.
        vtData = Inet1.GetChunk(1024, icByteArray)
        DoEvents
        
        If Len(vtData) = 0 Then
                bDone = True
        End If
        
        Do While Not bDone
            tempArray = vtData
            Put #1, , tempArray
                        
            ' Get next chunk.
            vtData = Inet1.GetChunk(1024, icByteArray)
            DoEvents
        
            If Len(vtData) = 0 Then
                bDone = True
            End If
        Loop
        
        Close #1
    End Select
End Sub 


REFERENCES

Microsoft Developer Network: Data Type Summary

Additional query words:


Keywords          : 
Version           : WINDOWS:6.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 29, 1999