Android Question httpjob unable to get the filename

ludogomez

Member
Licensed User
Longtime User
Hello,

I try to download files from a server (nextcloud server for example), but I don't know filename.

If I use a web browser, it retrieve the filename. But when I use b4a with httpjob, I don't have the filename.
I try getheaders, but without succes.

I join a small project.
You can try with this link : https://www.leslulus.com/nextcloud/index.php/s/stCoQ6WBTkqBXNx

Thanks
Regards
 

Attachments

  • test_download.zip
    8.5 KB · Views: 90

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
headers:content-disposition:[attachment; filename*=UTF-8''test.odt; filename="test.odt"]
This is a line of the headers when calling

B4X:
    Dim url As String = "https://www.leslulus.com/nextcloud/index.php/s/stCoQ6WBTkqBXNx/download"
   
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(url)
    '    j.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim resp As OkHttpResponse = j.Response
        Dim list1 As Map
        list1 = resp.GetHeaders
        For i = 0 To list1.Size - 1
            Log("headers:" & list1.GetKeyAt(i) & ":" & list1.GetValueAt(i))
        Next
           
        Dim filename As String
        filename = url.SubString(url.LastIndexOf("/"))
               
        Dim su As StringUtils
        filename = su.DecodeUrl(filename,"UTF8")
        Log(filename)
               
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, filename, False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
       
        Log(File.ListFiles(File.DirInternal))
   
    End If


headers:content-disposition:[attachment; filename*=UTF-8''test.odt; filename="test.odt"]
this is the headerline you need to parse and extract the filename

Note that i suffixed the url with "/download" to get this result
 
Upvote 0

ludogomez

Member
Licensed User
Longtime User
Thanks for the response,

I try with my server and I did not see the good header, my bad.

But for my app, I'll try another server but :
  • it's a microsoft tecnhology (ASPNET)
  • it's not my server
  • I must connect with a login/password
I can't reproduce easily this behaviour :

this the headers:

B4X:
headers:cache-control:[private]
headers:cf-ray:[5189114a1d0cc84f-AMS]
headers:content-encoding:[gzip]
headers:content-type:[text/html; charset=utf-8]
headers:date:[Thu, 19 Sep 2019 05:12:56 GMT]
headers:expect-ct:[max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"]
headers:server:[cloudflare]
headers:x-aspnet-version:[4.0.30319]
headers:x-content-type-options:[nosniff]
headers:x-powered-by:[ASP.NET]

Thanks
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…