How to return Datasnap Server image and show in browser

Asked

Viewed 650 times

1

I developed an application made in Web Service REST, where the data is consumed at each client request, everything works perfectly, I created the Web Service REST from the DataSnap REST Application, with this becomes available a form to determine the port I will use, but I had to migrate this application to a Serviço, the return of the image did not work with the same function that I use in the application of Web Service REST, I can return the image in png/jpeg. I created this service from DataSnap Server, including the library JavaScript. I will post the source of how to return the image on Web Service REST:

oFileStream:= TFileStream.Create(sCaminho, fmOpenRead or fmShareDenyWrite);
try
  if oFileStream.Size > 0 then
  begin
    SetLength(Result, oFileStream.Size);
    oFileStream.Read(Pointer(Result)^, oFileStream.Size);
  end;
 finally
   FreeAndNil(oFileStream);
end;

s := CarregarImagem(sCaminho);
GetInvocationMetadata(True).ResponseContentType := 'image/png';
GetInvocationMetadata(True).ResponseCode := 200;
GetInvocationMetadata(True).ResponseContent := s;
GetInvocationMetadata(True).CloseSession    := True;

I hope I made myself clear. Hugs!

  • 1

    From what I’ve seen you’re not working with Encode64. Try to encrypt the image and make a client to decode.

  • @Andrey, I’ll do it, thanks for the tip

  • @Andrey, I managed to solve converting the image to Base64 and tested in the Postman application, I will use Base64 because it is the standard when sending images to the customer read and view.

2 answers

1


Usually in the transmission of images via REST, is used to base64 to encrypt the image. Transmission in base64 is much lighter as it does not store information on cache.

1

I have a question about this topic. I have the following code working on the XE8:

foto := 'teste.jpg';

  if  FileExists(foto) then
  begin

    lStm := TStringStream.Create;
    lStm.LoadFromFile(foto);

    GetInvocationMetadata().ResponseContentType := 'image/jpeg';
    GetInvocationMetadata().ResponseContent := lStm.DataString;

    lStm.free;
  end;

this generates the following image:

Código funcionando perfeitamente no XE8

I also noticed that the Content Type generated is

ContentType (via postman)

However, in Tokyo 10.2, the same code generates the following image:

inserir a descrição da imagem aqui And the following content type inserir a descrição da imagem aqui Why does the same code not work in newer Delphi ? Is it a BUG of the new version? Or you have to pass some more parameter so that it format the image well ?

I tried to do the following on Tokyo 10.2 to "force" get the same contenttype that was sent on XE8, but to no avail:

GetInvocationMetadata().ResponseContentType := 'text/html; charset=ISO-8859-1, image/jpeg';

como podem ver, ele não pega o image/jpeg

Someone’s been through this trouble ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.