B4J Question [SOLVED] Do we have an example code to download huge files?

Mashiane

Expert
Licensed User
Longtime User
B4X:
Sub DownloadFile(Link As String, FileName As String) As ResumableSub
    Dim job As HttpJob
    job.Initialize(FileName, Me) 'note that the name parameter is no longer needed.
    job.Download(Link)
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        'save the file
        Try
            Dim OutStream As OutputStream
            OutStream = File.OpenOutput(File.DirApp, "Videos/" & FileName, False)
            File.Copy2(job.GetInputStream,OutStream)
            OutStream.Close
        Catch
            LogError($"DownloadFile: ${FileName} - ${LastException}"$)
        End Try
    Else
        LogError($"DownloadFile: ${Link}. ${job.ErrorMessage}"$)
    End If
    job.Release
    Return job.success
End Sub

Call It: (this is inside a loop)

B4X:
wait for (DownloadFile(url,vidName)) complete(Result As Boolean)
 
Upvote 0
Top