Linking ProgressBar to file download?

N1c0_ds

Active Member
Licensed User
I'm using the ProgressBar control to show the download progress for various CAB files downloaded from various servers. I will need a progress bar to show that the program is not dead but I don't know how to link it. Here's the DownloadFile sub:

B4X:
Sub DownloadFile (LocalFile,URL)
      Response.New1
      Request.New1(URL)
      Response.Value = Request.GetResponse 'Launch the request and get the response.
     Response.Value = Request.GetResponse 'Launch the request and get the response.
      Reader.New1(Response.GetStream,true) 'Use a BinaryFile object to read the data from the Response stream.
      FileOpen(c1,LocalFile,cRandom)
      Writer.New1(c1,false)
      Dim buffer(4096) As byte
      count = Reader.ReadBytes(buffer(),4096)
      Do While count > 0
            Writer.WriteBytes2(buffer(),0,count)
            count = Reader.ReadBytes(buffer(),4096)
      Loop
      FileClose(c1)     
      Response.Close 'Close the Response stream.
End Sub 'Get files
 

Erel

Administrator
Staff member
Licensed User
You can do something like:
B4X:
Sub DownloadFile (LocalFile,URL)
      Response.New1
      Request.New1(URL)
      Response.Value = Request.GetResponse 'Launch the request and get the response.
     Response.Value = Request.GetResponse 'Launch the request and get the response.
      Reader.New1(Response.GetStream,true) 'Use a BinaryFile object to read the data from the Response stream.
      FileOpen(c1,LocalFile,cRandom)
      Writer.New1(c1,false)
      Dim buffer(4096) As byte
      bar.Maximum = Reader.Length
      bar.value = 0
      bar.refresh
      count = Reader.ReadBytes(buffer(),4096)
      Do While count > 0
            Writer.WriteBytes2(buffer(),0,count)
            count = Reader.ReadBytes(buffer(),4096)
            bar.value = bar.value + count
            bar.refresh
      Loop
      FileClose(c1)     
      Response.Close 'Close the Response stream.
End Sub 'Get files
 

N1c0_ds

Active Member
Licensed User
You can do something like:
B4X:
Sub DownloadFile (LocalFile,URL)
      Response.New1
      Request.New1(URL)
      Response.Value = Request.GetResponse 'Launch the request and get the response.
     Response.Value = Request.GetResponse 'Launch the request and get the response.
      Reader.New1(Response.GetStream,true) 'Use a BinaryFile object to read the data from the Response stream.
      FileOpen(c1,LocalFile,cRandom)
      Writer.New1(c1,false)
      Dim buffer(4096) As byte
      [B][U][I][COLOR="RoyalBlue"]bar.Maximum = Reader.Length[/COLOR][/I][/U][/B]
      bar.value = 0
      bar.refresh
      count = Reader.ReadBytes(buffer(),4096)
      Do While count > 0
            Writer.WriteBytes2(buffer(),0,count)
            count = Reader.ReadBytes(buffer(),4096)
            bar.value = bar.value + count
            bar.refresh
      Loop
      FileClose(c1)     
      Response.Close 'Close the Response stream.
End Sub 'Get files
It produces an error here. I tested "bar.Response=Response.ContentLength" and it works perfectly.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
I guess you mean bar.Maximum=Response.ContentLength.
 
Top