HTTP: How to download a file

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I need to download a single file and I can't work out from the tutorial nor from the HelpViewer info on GetAsynchronously how to just download a file. It says:

GetAsynchronously (EventName As String, Output As java.io_OutputStream, CloseOutput As Boolean, TaskId As Int) As Boolean

This command does not pass the URL. In the "complicated" tutorial, I see where you are passing a URL in
request.InitializeGet(MainUrl)
HttpClient1.Execute(request, MainRequestId)
but then I don't see where you are passing the information to select and download an image file.

Here's what I've got. Can you tell me what to change to get it to work?

Sub Process_Globals
Dim URL As String
URL = "http://www.aeyec.com/aic.jpg"
Dim HttpClient1 As HttpClient
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim request As HttpRequest
Response.GetAsynchronously(URL, File.OpenOutput _
(File.DirInternalCache, "aic.jpg", False), True, TaskID
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Something like:
B4X:
Sub Process_Globals
Dim URL As String
URL = "http://www.aeyec.com/aic.jpg"
Dim HttpClient1 As HttpClient
End Sub

Sub Activity_Create(FirstTime As Boolean)
 If FirstTime Then
  HttpClient1.Initialize("Http")
 End If
Dim request As HttpRequest
request.InitializeGet(URL)
HttpClient1.Execute(request, 1)
End Sub

Sub Http_ResponseSuccess(Response As HttpResponse, TaskId As Int)
Response.GetAsynchronously("ResponseSub", File.OpenOutput _
(File.DirInternalCache, "aic.jpg", False), True, TaskID)
End Sub

Sub ResponseSub_StreamFinish (Success As Boolean, TaskId As Int)
 'work with downloaded file
End Sub
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I added your code the other day and it did the trick, but today I noticed something that may be a problem:

Edit: As noted in a message below, the following comments are incorrect. (I'm leaving them in so that the messages that follow make sense, unless the Mod can delete all of these.)

If I copy a file from DirAssets to DirDefaultExternal, it puts it (ultimately) in a .../Files folder, but when I download the file and specify DirDefaultExternal, it puts it in a .../Downloads folder.

It appears that the app uses the one in the .../Files folder, so I'm assuming that after my app downloads a file, it needs to copy it over from /Downloads to /Files.

Does this all sound plausible?
 
Last edited:
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Is it possible that you have a Files subfolder inside the Files folder (in your project source files)? There should not be any Files folder on the device.

As I was about to send a reply complete with screen shots, I suddenly realized what happened. When I was trying to get the file download to work, I created a small app just to do the download and named it -- guess what -- "Download".

That's what created that folder. I'll go back and edit my previous post to correct it. Sorry for the confusion.
 
Upvote 0
Top