Saving downloaded file

magarcan

Active Member
Licensed User
Longtime User
Hi all! I have a problem saving a file downloaded from Internet. If file already exists in the folder, file is added instead of rewrite it (for example if I download example.jpg that has 12kb, and then try downloading again in the same place, now example has 24kb).

This is code I'm using:
B4X:
Sub JobDone (Job As String)
   If HttpUtils.IsSuccess(fileurl&filename) Then
       Dim out As OutputStream
      out = File.OpenOutput(fd.FilePath,filename, True)
       File.Copy2(HttpUtils.GetInputStream(fileurl&filename), out)
       out.Close
      ProgressDialogHide
      ToastMessageShow(filename&" downloaded OK!", False)
   End If
End Sub

Any idea? Thanks!!
 

deantangNYP

Active Member
Licensed User
Longtime User
i am trying to learn how to Download and Save a file using httputils.
this is a working example...

B4X:
Sub Button4_Click
   Dim FileUrl As String
   FileUrl = "http://192.0.0.1/1234.xml"
     HttpUtils.CallbackActivity = "Main" 'Current activity name.
     HttpUtils.CallbackJobDoneSub = "JobDone"
     HttpUtils.Download("Job1", FileUrl)
End Sub

Sub JobDone (Job As String)
   Dim filename,fileurl As String
   fileurl = "http://192.0.0.1/"
   filename = "1234.xml"
   
    If HttpUtils.IsSuccess(fileurl&filename) Then
      Dim out As OutputStream
      out = File.OpenOutput(File.DirRootExternal,filename, False)
      File.Copy2(HttpUtils.GetInputStream(fileurl&filename), out)
      out.Close
      ProgressDialogHide
      ToastMessageShow(filename&" Downloaded OK!", False)
    End If
End Sub

Thanks.
 
Last edited:
Upvote 0

deantangNYP

Active Member
Licensed User
Longtime User
I recommend you to use HttpUtils2 instead of HttpUtils.

Thanks.
I suppose the following is the recommended option?

B4X:
Sub Button6_Click
   Dim job3 As HttpJob
   'Send a GET request    
   job3.Initialize("Job3", Me)    
   job3.Download("http://192.168.0.100/1234.xml")
   
End Sub

Sub JobDone (Job As HttpJob)
   Dim filename As String
   filename = "1234.xml"
   
    If Job.Success = True Then
      'Log(Job.GetString)
      File.WriteString(File.DirRootExternal, filename, Job.GetString)
      ToastMessageShow(filename&" Downloaded OK!", False)'
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 
Upvote 0

psdos

Active Member
Licensed User
Longtime User
I have a problem downloading file, by default system download it in internar memory, in Galaxy Ace, with 190MB of internal memory, i have error by no memory. Can i change download default path at SD_CARD ?

Thanks.
 
Upvote 0

mrwebbit

Member
Licensed User
Longtime User
is possible?

B4X:
            Dim out As OutputStream
            out = File.OpenOutput(File.DirInternal,"cane.jpg",False)
            File.Copy2(Job.GetInputStream,out)
            out.close
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…