Results 1 to 2 of 2

Thread: HTTP Protocol Downloading.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    Atlanta
    Posts
    104

    Question

    Ok, here's the situation..

    I want to download multiple files using the http protocol.

    I know how to download a webpage and save it to a file using the inet.openURL/byteArray method, I know how to ftp files using the inet.execute "get/put" commands, and I know how to do a dialog free download.

    But I'm unsatisfied using any of these methods to download an actual file (something larger then 10k) using http. Perhaps there is an easy answer that I've just overlooked, but...

    Anyway, here are the methods that I've tried. This is a nice tutorial even if I don't get an answer. =).

    Dialog Free Download method:
    This is nice, it's fast and useful for small files, but it disables the form while the file is being transfered. This means if it's downloading a large file it will halt the form altogether until it's completed that download. Not very useful for what I need.

    This is the code and usage, if there's any way to fix this please post.

    Code:
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
      "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As _
      String, ByVal szFileName As String, ByVal dwReserved As Long, _
      ByVal lpfnCB As Long) As Long
    
    For i = 0 to totaldownload
      DownloadFile ("http://foobar.file" & i, "c:\bleah.file")
    Next i
    
    Public Function DownloadFile(URL As String, LocalFilename As _
      String) As Boolean
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function
    Inet.OpenURL method:
    This works nicely, you can add a progress bar and trap any errors, but writing to the file takes almost as long as the download time. If you remove the "DoEvents" from the For/Next statement it's a bit faster but still takes a long time. This is very usefull if you are downloading a web page document or something small, but not for larger files.

    Code and usage. Any suggestions?

    Code:
    Dim byte As String
    Dim b() As Byte
    Dim o As Long
    
    For i = 0 to totaldownload
      b() = Inet.OpenURL("http://foobar.file" & i, icByteArray)
    
      Open "c:\bleah.file" For Output As #1
        For o = 0 To UBound(b) - 1
          DoEvents
          byte = byte + Chr(b(o))
        Next o
        Print #1, byte
      Close #1
    Next i

  2. #2
    Member FirePoweR's Avatar
    Join Date
    Apr 1999
    Posts
    32
    This is what I use to download off of the internet:
    Make sure you have a label on the form called "lblStats" if you want a percentage done.
    Code:
    Option Explicit
    Public DlFile As String
    
    Private Sub Inet1_StateChanged(ByVal State As Integer)
        Dim bDone As Boolean: bDone = False
        Dim Dat() As Byte, ff As Long
        Dim FileSize As Double
        
        Select Case State
            Case icResponseCompleted ' 12
                DoEvents
                Dat() = Inet1.GetChunk(1024, icByteArray)
                
                If UBound(Dat) <= 0 Then
                    bDone = True
                End If
                
                FileSize = Val(Inet1.GetHeader("Content-Length:"))
                
                fs = FreeFile
                'Make DlFile a public variable and put the file name in it
                Open DlFile For Binary Access Write As #ff
                
                Put #ff, , Dat()
                
                Do While Not bDone
                    Dat() = Inet1.GetChunk(1024, icByteArray)
                    
                    Put #ff, , Dat()
                    
                    'make a label for the status
                    If FileSize > 0 Then lblStats = Int((FileSize / LOF(1)) * 100) & "%"
                    
                    If UBound(Dat) <= 0 Then
                        'If so, set the flag to indicate that we are done
                        bDone = True
                    End If
                Loop
                Close #ff
        End Select
    End Sub
    
    Public Function CheckPath(FilePath As String) As Boolean
        FilePath = UCase(Trim(FilePath))
        On Error GoTo ErrH
        If (GetAttr(FilePath) And vbDirectory) <> 0 Then
            CheckPath = True
            Exit Function
        End If
        If Dir(FilePath) <> "" Then
            CheckPath = True
        Else
            CheckPath = False
        End If
        Exit Function
    ErrH:
        CheckPath = False
    End Function
    
    Public Sub DownloadFile(URL As String, LocalFilename As String)
        Dim Headerz As String
        
        Headerz = "Referer: " & URL & vbCrLf & "User-Agent: PowerWeb (compatible; MSIE 4.01; Windows 98)" & vbCrLf & vbCrLf
        DlFile = LocalFilename
        If CheckPath(DlFile) Then Kill DlFile
        Inet1.Execute URL, "GET", , vbCrLf & vbCrLf & Headerz
        Do Until Inet1.StillExecuting = False
            DoEvents
        Loop
    End Sub
    If you want to be able to still use the form while its downloading then take out this in the DownloadFile function:
    Code:
        Do Until Inet1.StillExecuting = False
            DoEvents
        Loop
    Hope that helps...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width