#1 2018-11-10 18:17:00

deitysou
Member
Registered: 2018-11-10
Posts: 6

Sending/Retrieving Images between Client and Service

Hi,

I'm trying to upload Images from a client to a service vice versa.

I'm using the MVC so there's an interface defined which is shared by the service and the client.

I tried to send a image from the client like this (for testing purpose):

varvar
  LPicture     : TSynPicture;
  LIntf         : IAccount;
  LRawBlob   : TSQLRawBlob;
  LRawBytes : RawByteString;
begin
...
    try
      LPicture := TSynPicture.Create;
      LPicture.LoadFromFile(OpenDialog1.FileName);

      if LPicture.Empty then
      begin
        Exit;
      end;

      SaveAsRawByteString(LPicture, LRawBytes, gptPNG, 80, 300);

      LRawBlob := BlobToTSQLRawBlob(LRawBytes);

      if FClient.Services['Account'].Get(LIntf) then
      begin
        LIntf.UploadPicture(1, LRawBlob, 1);
      end;
    finally
      FreeAndNil(LPicture);
    end;

I do receive the LRawBlob field at my service but the bytstream is corrupte I think I need to encode the stream as Base64.

So here's my question:
If I have to manually encode the LRawBytes as Base64 wouldn't it be much easier to use RawUTF8 instead of TSQLRawBlob or do I miss something and there's some magic I could use?

P.S.: @ab: Greetings from the EKON22 I'm still missing your EKON example code. big_smile

Last edited by deitysou (2018-11-10 18:19:24)

Offline

#2 2018-11-12 07:58:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,243
Website

Re: Sending/Retrieving Images between Client and Service

How is your IAccount defined?

To upload efficiently some binary, better define a single RawByteString parameter (without the 1, ..., 1 other values), so that JSON marshalling will be by-passed and no Base64 encoding will be made.
See https://synopse.info/files/html/Synopse … l#TITL_197

Offline

#3 2018-11-12 12:54:44

deitysou
Member
Registered: 2018-11-10
Posts: 6

Re: Sending/Retrieving Images between Client and Service

Thank you.

My Interface is defined as:

    IAccount = interface(IInvokable)
      ...
      function UploadPicture(const sortid: Integer; picture: TSQLRawBlob): Boolean;
    end;

So the new code is implemented like this:

  function TDfXAccount.UploadPicture(const sortid: Integer; picture:
    TSQLRawBlob): Boolean;

At least I need one id (sortid) for this function call. Is there a way to keep this parameter?

Offline

#4 2018-11-12 14:01:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,243
Website

Re: Sending/Retrieving Images between Client and Service

You can append to the binary, or use a set of dedicated methods UploadPictureOneSort, UploadPictureAnotherSort...

Offline

#5 2018-11-12 16:07:18

deitysou
Member
Registered: 2018-11-10
Posts: 6

Re: Sending/Retrieving Images between Client and Service

Thank you I'll try that.

And thank you for uploading the EKON example.

Offline

#6 2018-11-12 21:56:10

deitysou
Member
Registered: 2018-11-10
Posts: 6

Re: Sending/Retrieving Images between Client and Service

I tried your advice and it worked well until I set the OnlyJSONRequests option true.
I need to have all clients to send JSON only so how could I realize that?

Also I noticed that my Delphiclient (which is using the interfaces) sends request like "Account.Login" instead of "Account/Login".
Can I disable this behavior?

Also I tried to use the JWTForUnauthenticatedRequest but I didn't figure out how to omit auth check for some functions like Account/Register and Account/Login
where no JWT is available.

Sorry for bothering you. smile

Last edited by deitysou (2018-11-13 12:09:18)

Offline

#7 2018-11-13 14:31:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,243
Website

Re: Sending/Retrieving Images between Client and Service

I understand your requirement the other way round, sorry.
I thought it was about avoiding JSON/Base64 conversion.
So if you use RawByteString with other parameters (as you did first) it will make an hidden Base64 encoding/decoding as you expect.

Offline

Board footer

Powered by FluxBB