Android Question How to download and save files with HttpUtil2

Maco

New Member
Hi ,

In my application, I am trying to download files with HttpUtil2. I can download them, but I don't know how to save these files with their original filename.

For example, there are three files on my http server: 1.jpg; 2.jpg; test.jpg
I create a Job to download them, but I don't know how to save them from a outstream using their original file names. Could you please advise?

a part of code like this:

For i = 0 to 2
myURL = "http://basic4app.com/my" & FileName(i) ' myFileName contains the file names that
' I want to download from server.
Job1.Download(myURL)
Next



Sub JobDone(Job As HttpJob)
Dim OutStream As OutputStream
If Job.Success = True Then
OutStream = File.OpenOutput(File.DirInternal, fileName, False)
File.Copy2(Job.GetInputStream,OutStream) ' save the file
OutStream.Close
End If
End Sub

in above sample, how can I know which filename I should use? Thanks.

Alternatively, please adivse me other way to download files and save them with the original file name in my specified folder.
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim files As List
    files.Initialize
    files.Add("01.jpg") ' These files should be downloaded. They are set in a list to get them in a for-loop
    files.Add("02.jpg")
    files.Add("03.jpg")

    For i = 0 To files.Size - 1
        ' For each file in the fileLIST create a job
        Dim job As HttpJob
        Dim dummy As String
        dummy = files.Get(i)
        job.Initialize("DownloadImage", Me)
        job.Tag = dummy ' The filename are stored in the "Tag"-Property of the job so later you can "read" the filename from the Tag
        job.download("http://www.ilikepool.de/images/"&dummy) ' starts download...
    Next


B4X:
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("JobName: "&Job.JobName&" / Tag = "&Job.Tag)
   
        If Job.JobName = "DownloadImage" Then
            ' Thats the name of this special download we created using the Tag-Property
            Dim OutStream As OutputStream
            Log("DownloadReady: "&Job.Tag)
            OutStream = File.OpenOutput(File.DirInternal, Job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
            File.Copy2(Job.GetInputStream,OutStream) ' save the file
            OutStream.Close
            Log(Job.Tag&" are written to "&File.DirInternal) ' Write the Originalname to the log to see what happens ;-)
        Else
            ToastMessageShow(Job.JobName&": " & Job.GetString, True)
        End If
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Installing file.
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
startService: class anywheresoftware.b4a.samples.httputils2.httputils2service
startService: class anywheresoftware.b4a.samples.httputils2.httputils2service
startService: class anywheresoftware.b4a.samples.httputils2.httputils2service
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Activity (main) Resume **
JobName: DownloadImage / Tag = 02.jpg
DownloadReady: 02.jpg
02.jpg are written to /data/data/b4a.example/files
JobName: DownloadImage / Tag = 01.jpg
DownloadReady: 01.jpg
01.jpg are written to /data/data/b4a.example/files
JobName: DownloadImage / Tag = 03.jpg
DownloadReady: 03.jpg
03.jpg are written to /data/data/b4a.example/files


Please note that the Dim and initialisation must be inside the FOR-Loop

!!! WRONG !!!
B4X:
  Dim job As HttpJob
  job.Initialize("DownloadImage", Me)
  For i = 0 To files.Size - 1
        job.Tag = dummy ' The filename are stored in the "Tag"-Property of the job so later you can "read" the filename from the Tag
        job.download("http://www.ilikepool.de/images/"&dummy) ' starts download...
    Next

!!! RIGHT !!!
B4X:
  For i = 0 To files.Size - 1
    Dim job As HttpJob
    job.Initialize("DownloadImage", Me)
    job.Tag = dummy ' The filename are stored in the "Tag"-Property of the job so later you can "read" the filename from the Tag
    job.download("http://www.ilikepool.de/images/"&dummy) ' starts download...
Next
 

Attachments

  • downloadimagestest.zip
    6.3 KB · Views: 1,240
Last edited:
Upvote 0
Top