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:

Alex_197

Well-Known Member
Licensed User
Longtime User
Hello did you get the file downloaded? I need that solution
B4X:
private Sub DownloadPhotoHTTP()
    
    Try
        Dim J As HttpJob
        Dim URL As String
        Dim F As Boolean,FL As Boolean
        Dim S As Int,FS As Int
        Dim MySQL As String
        Dim Cursor1 As Cursor
        Dim Res As Boolean
        Dim Photo As String
        Dim Logo As String
        
        
        MySQL="select FacilityID,FacilityName,ProviderID,UserID,ProviderPhoto,ProviderName,Address,City,State,Zip,Phone,Logo from tblFacility order by FacilityName"
        
        Cursor1=SQL1.ExecQuery(MySQL)
        
        
        
        For i=0 To Cursor1.RowCount-1
                        
            Cursor1.Position=i
            
            Photo=Cursor1.GetString("ProviderPhoto")
            
            If Photo="" Then
                Continue   
            End If
                        
            F=File.Exists(Main.filedir, Photo)
            S=File.Size(Main.filedir, Photo)
            
            
            If F = False Or s=0 Then
                URL= "http://www.servername.com/"& Photo
        
                j.Initialize("", Me)
        
                j.Download(URL)
        
                Wait For (j) JobDone(j As HttpJob)
        
                If j.Success Then
            
                    Dim out As OutputStream = File.OpenOutput(Main.FileDir, Photo, False)
                    File.Copy2(j.GetInputStream, out)
                    out.Close '<------ very important
                    Log("Photo " & Photo & " downloaded Ok")
                    Main.TestRet=True
                Else
                
                    modFun.ShowError("clsRefreshResume_HTTP Download error " &  CRLF & "Try again later with better connection and check if photo exists on a provider profile.")
                    Main.TestRet=False
                End If
        
                j.Release
            
            End If
            
        
        Next
        
        i=0
        
        For i=0 To Cursor1.RowCount-1
                        
            Cursor1.Position=i
            
            Logo=Cursor1.GetString("Logo")
            
            If Logo="" Then
                Continue
            End If
            
                        
            FL=File.Exists(Main.filedir, Logo)
            FS=File.Size(Main.filedir, Logo)
            
        
            If FL = False Or FS=0 Then
                
                URL= modFun.URLImage & Logo
        
                j.Initialize("", Me)
        
                j.Download(URL)
        
                Wait For (j) JobDone(j As HttpJob)
        
                If j.Success Then
            
                    Dim out As OutputStream = File.OpenOutput(Main.FileDir, Logo, False)
                    File.Copy2(j.GetInputStream, out)
                    out.Close '<------ very important
                    Log("Logo " & Logo & " downloaded Ok")
                    Main.TestRet=True
                Else
                
                    modFun.ShowError("clsRefreshResume_HTTP Download error. Try again later with better connection. Also check if the profile has a photo.")
                    Main.TestRet=False
                End If
        
                j.Release
            
            End If
            
        Next
        
        Cursor1.Close
        
        Log("DownloadPhotoHTTP " & Main.TestRet)
        CallSubDelayed(Me,"DownloadPhotoHTTP_Complete")
        
    Catch
        Log("DownloadPhotoHTTP " & LastException)
        modFun.ShowError("clsRefreshResume_DownloadPhotoHTTP " & LastException)
        Main.TestRet=False
    End Try
    
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hello did you get the file downloaded? I need that solution
Posting to OLD threads is a mistake.

You should ALWAYS create a new thread for any Question/Issue you have,
 
Upvote 0
Status
Not open for further replies.
Top