How to return image from DataSnap Server and show in browser

1

I developed an application made with Web Service REST , where the data is consumed for each client request, everything works perfectly, I created Web Service REST from DataSnap REST Application , so a form is available to determine the port that I'm going to use, but I had to migrate that application to a Serviço , the image return did not work with the same function that I use in the Web Service REST application, I can return the image in png/jpeg . I created this service from DataSnap Server , including the JavaScript library. I will post the source of how do I return the image in 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 have been clear. Hugs!

    
asked by anonymous 21.10.2016 / 15:56

2 answers

1

Generally in the transmission of images via REST , base64 is used to encrypt the image. Transmission in base64 is much lighter because it does not save information in cache .

    
21.10.2016 / 20:30
1

I have a question about this. I have the following code working in delphi 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:

IalsonoticedthattheContentTypeitgeneratedis

However,inTokyo10.2,thesamecodegeneratesthefollowingimage:

Andthefollowingcontenttype WhydoesnotthesamecodeworkonDelphi?IsitaBUGofthenewversion?Ordoyouhavetopasssomemoreparameterstofittheimage?

Itriedtomaketokyo10.2thefollowingto"force" to get the same contentType that was sent in XE8, but to no avail:

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

Has anyone ever had this problem?

    
21.07.2018 / 19:51