Android Question upload document to server

Jocelin

Member
Licensed User
Hello!

I want to convert an image to pdf and I want to send this image to a server.
I can create a pdf document, but i cannot send it to a server.
I don't know if I use good code and good object.
Thank you for your help.

B4X:
private Sub uploadPicture
    
    Dim newimage As Bitmap
    Dim rect As Rect
    Dim pdf As PdfDocument
    
    Dim req As HttpRequest
    Dim reqClient As HttpClient
    Dim size As Long
    
    pdf.Initialize
    newimage = LoadBitmapResize(File.DirRootExternal&"/Eyevu_data","1.png",680,920,True)
    rect.Initialize(0,0,newimage.Width,newimage.Height)
    
    pdf.StartPage(1, 1) 'A4 size
    pdf.Canvas.DrawBitmap(newimage,rect, rect)
    
    'Oncomment this line for test with text
    '    pdf.Canvas.DrawLine(2, 2, 593 , 840, Colors.Blue, 4)
    '    pdf.Canvas.DrawText("Hello", 200, 200, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.Yellow, "CENTER")
    
    pdf.FinishPage
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "1.pdf", False)
    pdf.WriteToStream(out)
    out.Close
    pdf.Close
    

    reqClient.Initialize("")
    size = File.size(File.DirRootExternal, "1.pdf")
    Dim InputStream1 As InputStream = File.OpenInput(File.DirRootExternal, "1.pdf")
    
    Globals.last_url = Params.get_eyevu_url & "/mobint/setdocument.php?id="&"&cid="& clientId &"&jwt=" & Params.get_jwt_token
    req.InitializePost(Globals.last_url, InputStream1, size)
    reqClient.Execute(req, 1)

end Sub
 

DonManfred

Expert
Licensed User
Longtime User
Dim req As HttpRequest
Dim reqClient As HttpClient
It is most probably the wrrong way to use the httpclient directly.

You should use okhttp and okhttputils library
 
Upvote 0

Jocelin

Member
Licensed User
Thank you for your response.
I use B4A and is for web server.

Usually, I use this for do a download from web server to a cellphone:

B4X:
Globals.classObject = Me
Globals.calllBackFunction =  "clientExamDone"
HttpUtils.CallbackJobDoneSub = "handleHttpCallbackForClasses"
   
Globals.last_url = Params.get_eyevu_url & "/mobile/getclientlist.php?cid=" & clientId &"&jwt=" & Params.get_jwt_token
HttpUtils.Download("clientList",Globals.last_url)

Otherwise, for upload a pdf document from cellphone to web server, I don't know how do that.

I want to send a pdf document from the cellphone to web server.

Thank you
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
I want to send a pdf document from the cellphone to web server.
u need a web server that will accept your file transfer as mentioned above.
this can be .php (need php interpreter installed) or ms web api (this need Internet Information Services (IIS), web api is a project type in visual studio 2017)
if you have access to your web server pc you can also use B4J there with our own web server app.
from B4A you need send something that the opposite understand.
send as multipart form data via post have a little bit complicated overhead but it works in web.
this okhttputils library which was are part of B4A ide setup can handle this.
 
Upvote 0

Jocelin

Member
Licensed User
I found how to put an image inside a pdf document and send the pdf on a web server. Thank you for your responses.

B4X:
private Sub uploadPicture
  
    Dim newimage As Bitmap
    Dim rect As Rect
    Dim pdf As PdfDocument
  
    Dim drId As String = ""
    Dim dtype As String = ""
    Dim rtype As String = ""
    Dim comments As String = ""
  
    pdf.Initialize
    newimage = LoadBitmapResize(File.DirRootExternal&"/myDocument","1.png",680,920,True)
    rect.Initialize(0,0,newimage.Width,newimage.Height)

    pdf.StartPage(680,920)
    pdf.Canvas.DrawBitmap(newimage,rect, rect)
  
    pdf.FinishPage
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal&"/myDocument","1.pdf", False)
    pdf.WriteToStream(out)
    out.Close
    pdf.Close
  
    Dim imgbytes() As Byte = FileToBytes(File.DirRootExternal&"/myDocument","1.pdf")
  
    Globals.classObject = Me
    Globals.calllBackFunction =  "uploadDone"
    HttpUtils.CallbackJobDoneSub = "handleHttpCallbackForClasses"
    Globals.last_url = Params.get_eyevu_url & "/mobile/setdocClient.php?id="&drId&"&cid="&client.GetNoClient()&"&datec="&datereservation&"&fext=pdf"&"&dtype="&dtype&"&rtype="&rtype
  
    HttpUtils.PostBytes("savephoto",Globals.last_url,imgbytes)

End Sub


Sub FileToBytes (Dir As String, FileName As String) As Byte()
    Return Bit.InputStreamToBytes(File.OpenInput(Dir, FileName))
End Sub


Private Sub uploadDone(Job As String)

End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Globals.classObject = Me
Globals.calllBackFunction =
"uploadDone"
HttpUtils.CallbackJobDoneSub = "handleHttpCallbackForClasses"
Globals.last_url = Params.get_eyevu_url & "/mobile/setdocClient.php?id="&drId&"&cid="&client.GetNoClient()&"&datec="&datereservation&"&fext=pdf"&"&dtype="&dtype&"&rtype="&rtype
I've been lurking here for over a year and have never seen code like this for uploading. What is this?
 
Upvote 0
Top