B4J Question B4J Server - download file

astronald

Active Member
Licensed User
Longtime User
Hello everybody
Please help me,
I have a b4j server my user can download file from repository on dropbox, i get path from database then download file this works well, the problem happens when the second file is downloaded, 404 error is showed,I don't know why it happens

this is file link examples
https://jdb.rocas.co/drive?i=838986
https://jdb.rocas.co/drive?i=838987
https://jdb.rocas.co/drive?i=838988

this is my handler code

B4J Server Dropbox File download:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Procesar(req,resp)
    StartMessageLoop
End Sub

Sub Procesar(req As ServletRequest,resp As ServletResponse)
    Dim i As String = req.GetParameter("i")

        i  = req.GetParameter("i")
        Dim con As SQL
        con.Initialize("net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://serverendpoint:1433/doc;instanceName=SQLEXPRESS;user=sa;password=password" )
        Dim rs As ResultSet = con.ExecQuery2("{CALL usp_Ruta_Traer(?)}", Array(i))
        If rs.NextRow Then
            Dim n as string = rs.GetString("Ruta")
        End If
        If rs.IsInitialized Then  rs.Close
        If con.IsInitialized Then  con.Close
        If n <> "" Then
            Dim j As HttpJob
            Try
           
                j.Initialize("", Me)
                j.GetRequest.Timeout = 60000
                j.PostString ("https://content.dropboxapi.com/2/files/download","")

                j.GetRequest().SetContentType("application/octet-stream")
                j.GetRequest().SetHeader ("Authorization", "Bearer XXXXXXXXXXXapidropboxXXXXXXXX")
                j.GetRequest().SetHeader ("Dropbox-API-Arg", "{""path"":""" & n & """}")
'                Log("Ruta: " &  n)
                Wait For(j) JobDone(j As HttpJob)
                If j.Success Then
'                    Log("descargando archivo")
                    Dim b() As Byte=Bit.InputStreamToBytes(j.GetInputStream)
                    Dim x As Int = n.LastIndexOf("/")
                    n = n.SubString(x+1)
                    resp.SetHeader("Content-disposition", $"attachment; filename=${n}"$)
                    resp.SetHeader("Cache-Control", "no-cache, no-store") ' disables cache for HTTP 1.1
                    resp.SetHeader("Pragma", "no-cache") ' disables cache for HTTP 1.0

                    resp.ContentType="application/octet-stream"
                    resp.ContentLength=b.Length
               
                    Log(n)
                    resp.OutputStream.WriteBytes(b,0,b.Length)
                    resp.OutputStream.Close
               
                Else
                    resp.Status=404
                End If
            Catch
                resp.Status=404
                Log(LastException)
            End Try
            j.Release
            StopMessageLoop
        End If
End Sub

I appreciate your help
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
This may of help


Additionally: Why are you using Poststring? You are not POSTing any usefull.

A download in my Dropbox HTTP Class uses job.Download for a download....
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
thanks Don Mainfred
Dont works, error :
java.lang.RuntimeException: Request does not support this method.
this is my code
Modified:
                j.Initialize("", Me)
                j.Download ("https://content.dropboxapi.com/2/files/download")
                j.GetRequest.Timeout = 60000
                j.GetRequest().SetContentType("application/octet-stream")
                j.GetRequest().SetHeader ("Authorization", "Bearer xxxTokenDropboxxxx")
                j.GetRequest().SetHeader ("Dropbox-API-Arg", "{""path"":""" & n & """}")
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
' Download a file from a user's Dropbox.
Sub Download(path As String, destpath As String, destfile As String)
    Dim job As HttpJob
    job.Initialize("filedownload",Me)
    job.Tag = File.Combine(destpath,destfile)
    LogColor("Download: "&job.Tag,Colors.Blue)
    job.Download("https://content.dropboxapi.com/2/files/download")
    job.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken)
    job.GetRequest.SetHeader("Dropbox-API-Arg", $"{"path":"${path}"}"$)
    job.GetRequest.SetHeader("Accept","application/vnd.dropbox-cors-hack")
End Sub
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
Thanks Don,
java.lang.RuntimeException: Request does not support this method.
again
But i think this is not error, becouse on my machine work's great, when i publish on vps, it works sometimes, please try links on first post

Thanks for your time.
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
Thanks Oliver,Don
Now works better but sometimes, show this error
Error in call to API function "files/download": Must provide HTTP header "Authorization" or URL parameter "authorization".

With Download:
    Try
        j.Initialize("", Me)
        j.Download ("https://content.dropboxapi.com/2/files/download")
        j.GetRequest().SetHeader ("Authorization", "Bearer xxxxTokenxxxx")
        j.GetRequest().SetHeader ("Dropbox-API-Arg", $"{"path":"${n}"}"$)
        j.GetRequest.SetHeader("Accept","application/vnd.dropbox-cors-hack")
       
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Dim b() As Byte=Bit.InputStreamToBytes(j.GetInputStream)
            Dim x As Int = n.LastIndexOf("/")
            n = n.SubString(x+1)
            resp.SetHeader("Cache-Control", "no-cache, no-store") ' disables cache for HTTP 1.1
            resp.SetHeader("Content-disposition", $"attachment; filename=${n}"$)
            resp.ContentType="application/octet-stream"
            resp.ContentLength=b.Length
            resp.OutputStream.WriteBytes(b,0,b.Length)
            resp.OutputStream.Close

                   
        Else
            resp.Status=404
            resp.Write("NO se encontro el archivo en el repositorio: " & j.ErrorMessage)
        End If
    Catch
        resp.Status=404
        resp.Write("Error al traer el archivo: "&LastException.Message)
    End Try
    j.Release
    StopMessageLoop

I think I should use startloop and stoploop, but I don't know how
Thanks.
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
Don't work always, how i can send authorization on parameter url, thanks.
"Error in call to API function "files/download": Must provide HTTP header "Authorization" or URL parameter "authorization".
 
Upvote 0
Top