B4A Library [B4X] RangeDownloader - resumable downloads

1628754839844.png


RangeDownloader uses http range feature to download the file in chunks. It will resume the download from the previous point, even if the app was previously killed.
It first sends a HEAD request to test whether this feature is supported.
Note that you need to delete the target file if you want to restart the download.

Supported by: B4A, B4i and B4J.

Usage example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Downloader As RangeDownloader
    Private AnotherProgressBar1 As AnotherProgressBar 'XUI Views
    Private url As String = "https://sabnzbd.org/tests/internetspeed/20MB.bin"
    Private Label1 As B4XView
End Sub

Public Sub Initialize
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xui.SetDataFolder("Download large file")
    Downloader.Initialize
    Dim tracker As RangeDownloadTracker = Downloader.CreateTracker
    Track(tracker)
    Wait For (Downloader.Download(xui.DefaultFolder, "test2.zip", url, tracker)) Complete (Success As Boolean)
    Log("Complete, success = " & Success)
    AnotherProgressBar1.Visible = False
End Sub

Private Sub Track (Tracker As RangeDownloadTracker)
    Do While Tracker.Completed = False
        Sleep(100)
        Label1.Text = $"$1.2{Tracker.CurrentLength / 1024 / 1024}MB / $1.2{Tracker.TotalLength / 1024 / 1024}MB"$
        AnotherProgressBar1.Value = Tracker.CurrentLength / Tracker.TotalLength * 100
    Loop
End Sub
 

Attachments

  • RangeDownloader.b4xlib
    1.3 KB · Views: 807
Last edited:

cbal03

Member
Licensed User
Longtime User
If you are asking about the progress tracking then it is possible with: https://www.b4x.com/android/forum/t...ts-file-uploads-with-progress.130181/#content (this example creates a multipart file upload).

Resumable uploads are not possible as there is no such feature in http protocol. If you are uploading files to your server then it shouldn't be too difficult to implement a similar protocol and upload the file in chunks.
I use php on my server to catch commands from my mobile to upload images, download images and scanned pdfs, query a database etc.. b4a was used for the device software. It shouldn't be too dificult to track uploaded file chunks for resumable uploads.
 
Top