Return image by Web Service REST Server

2

I have Web Service REST running, this web service makes the query in ERP when the client requests the request of a certain data, needed to return the image that the client made the request and display in the browser.

I made the following code that returns a stream, but I still did not succeed.

  

Result: = TFileStream.Create (sDirImage, fmOpenRead or fmShareCompat);

    
asked by anonymous 13.10.2016 / 19:43

1 answer

1

To view the right image in the browser, I was able to solve it as follows, I used the GetInvocationMetaData function, I have to specify for the browser what the return is. I'll put an example of how I did it:

declare non usues Data.DBXPlatform

function CarregarImagem(const sCaminho: String): AnsiString;
var
  oFileStream : TFileStream;
begin
  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;

end;

sImagem := CarregarImagem(sDirImagem);       
if (bExtPNG) then
  GetInvocationMetadata().ResponseContentType := 'image/png'
else
begin
  GetInvocationMetadata().ResponseContentType := 'image/jpeg';
  GetInvocationMetadata().ResponseCode := 200;
  GetInvocationMetadata().ResponseContent := sImagem;
  GetInvocationMetadata().CloseSession    := True;
end;  
    
13.10.2016 / 21:09