B4J Question web server - send bitmaps

LucaMs

Expert
Licensed User
Longtime User
Not exactly. Check index.html. There is no javascript there. There is however a form that is being send when the user presses on the submit button. This form is sent to filehelper.

In your case it is simpler. You don't need to handle the multipart data. Just create a standard handler that reads the file from the stream and send the file with HttpJob. After the file is sent you can send a notification to the websocket.

The same is true for files being sent from the server. Send a notification with the web socket and then download the file with HttpJob.

I had been able to send bitmaps directly through web sockets but using Base64 strings, which weigh 33% more than an array of bytes.

1) I do not know how to implement this Erel's suggestion

2) I have seen how the example WebSocketWithFileUpload works; it uses a Handler class, which receives an object of type ServletRequest; can I create this type of object in B4A?

Obviously, I would prefer the Erel's suggestion (1), but how?


Thank you
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
The only way to send the bitmap through the web socket is with base 64 encoding. A more efficient solution is to send a regular request (HttpJob.Download) from the client to download the image.

Thank you Erel.


I know (since your answer at that date :() that I should use a "http way", but I don't know how.

I need to exchange bitmaps between my b4j web socket server and b4a client; then, there is a web socket handler in my server, which receives also data to identify users.

As you know, there is a folder www, the only one accessible (hoping that hackers cannot access other folders :)).

To send a file from server to client I could use ws to "force" the client to download a file (using ws I'm (pretty) sure che user is already authorized), so the client could use some code like this (from an example on site):

B4X:
   'Send a GET request
   job3.Initialize("Job3", Me)
   job3.Download("http://www.b4x.com/forum/images/categories/android.png")

but what the URL should be? And the file should be available only for that user and only at that time.

And I have no idea about the upload (client--> server).

The examples I found on site are for browsers, which use java stuff.


Thank you
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Client(b4a) upload file to Server(b4j):
https://www.b4x.com/android/forum/t...r-b4j-server-over-the-internet.37201/#content

Client(b4a) download file from Server(b4j):
If the file is under the www folder (say, /www/download/yourfilename.zip), it's uri is http://192.168.x.xx:port/download/yourfilename.zip. Download it with httputils2(b4a)

For the upload, I took the code I posted in #5 right from the first link (but using the source code of httputils2).

The problem is the download; using that way, anyone (also non authorized people) can take files.


Thank you
 
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
Add a filter handler for /download folder. The user should get the auth info before downloading.

B4X:
srvr.AddFilter("/download/*","downloadFilter",False)

'Return True to allow the request to proceed.
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
    'get fileuri /download/filename.zip
    Dim fileuri As String = req.RequestURI
  
  'get auth info from headers  
    Dim userid As String=req.GetHeader("userid")
    Dim userauth As String=req.GetHeader("userauth")
  
    'get auth info from url  /download/filename.zip?userauth=xyz
    Dim userauth As String=req.GetParameter("userauth")
  
    'check the auth info ; return false if fail
  
    Return True
  
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I have some doubts (as usual).

If I use base64 for the exchange of images (client <---> server) I have the advantage of using the Web Socket and the simplified code (with user already identified).
However, this method implies a greater number of bytes sent and stored (DB).

Using the other method, the code is more complicated and the server must verify each time the user's credentials (I think I should add a filter also for the upload).

Is perhaps the first method (web socket) better?


[I think to send "little" images (max 100x100 px) only, no other kind of files]
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using the other method, the code is more complicated and the server must verify each time the user's credentials (I think I should add a filter also for the upload).
You can check the existing session and see whether the user has already authenticated.

If the files are small enough then you can send them through the web socket.
 
Upvote 0
Top