Android Question How to download file from remote web server and saved in download folder of mobile?

Status
Not open for further replies.

Rajesh Gupta

Member
Licensed User
Hello sir,
My code is
B4X:
Sub Process_Globals
    Dim job7 As HttpJob
End Sub

Sub Global
Dim FileName As String
End Sub

Public Sub downLoadFile()
    FileName="a.txt"
    Dim  FilePath As String="http://192.168.0.36/UWS1/UWS/Temp/a.txt"
    job7.Initialize("job7", Me)
    job7.Download(path)
End Sub

Sub JobDone (job As HttpJob)
If job.JobName="job7" Then
        If job.Success Then
            Log(job.Tag)
            Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, FileName, False)
            File.Copy2(job.GetInputStream, out)
            out.Close 
        
        Else
            Log("Error: " & job.ErrorMessage)
        End If
    Else
    
        Log("Error: " & job.ErrorMessage)
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
    
    job.Release
End Sub

When using above code, i didn't get any file, i.e. no any file downloaded.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Use code tags when posting code! You get told this in the past.

i didn't get any file
B4X:
Dim FilePath As String="http://192.168.0.36/UWS1/UWS/Temp/a.txt"
will only be accessible from within the same network.
So you need to be connected to the same network as the server. Using Wifi.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but log showed content length [0]
There is no such LOG command in your code.

If you dont get any error then Job.success must be true.

Upload a project which shows the issue.

Without it i can´t help any further.
 
Upvote 0

Rajesh Gupta

Member
Licensed User
There is no such LOG command in your code.

If you dont get any error then Job.success must be true.

Upload a project which shows the issue.

Without it i can´t help any further.

I upload my project with name Download_File
 

Attachments

  • Download_File.jpg
    Download_File.jpg
    200.4 KB · Views: 381
Upvote 0

Rajesh Gupta

Member
Licensed User
Tip: if you are using B4A v7+: [B4X] OkHttpUtils2 with Wait For
You can use File.Copy2 with Job.GetInputStream to copy the file to wherever you like (don't forget to close the OutputStream).

Sir I have already done it but still not get any downloaded file.:(

B4X:
Sub JobDone (job As HttpJob)
    If job.JobName="job7" Then
        If job.Success Then
            Log(job.GetString)
            Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, FileName, False)
            File.Copy2(job.GetInputStream, out)
            out.Close
       
        Else
            Log("Error: " & job.ErrorMessage)
        End If
    Else
   
        Log("Error: " & job.ErrorMessage)
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
   
    job.Release
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i´m not sure what is the issue here but you should not use a process global job.
B4X:
Sub Process_Globals
End Sub

Sub Global
Dim FileName As String
End Sub

Public Sub downLoadFile()
    FileName="a.txt"
    Dim  FilePath As String="http://192.168.0.36/UWS1/UWS/Temp/a.txt"
    Dim job7 As HttpJob ' DIM the job here....
    job7.Initialize("job7", Me)
    job7.Download(path)
End Sub

Sub JobDone (job As HttpJob)
If job.JobName="job7" Then
        If job.Success Then
            Log("success:"&job.Tag)
            Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, FileName, False)
            File.Copy2(job.GetInputStream, out)
            out.Close
       
        Else
            Log("Error: " & job.ErrorMessage)
        End If
    Else
        Log("Error: " & job.ErrorMessage)
        ToastMessageShow("Error: " & job.ErrorMessage, True)
    End If
   
    job.Release
End Sub
 
Upvote 0
Status
Not open for further replies.
Top