Return image by Web Service REST Server

Asked

Viewed 714 times

2

I have a Web Service REST running, this web service does the query on ERP when the client requests the request of a given data, it needed to return the image that the client made the request and show in the browser.

I did the following code that returns a stream, but I was still unsuccessful.

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

  • Look, I can’t tell you if that’s your problem, but I’ve had trouble moving images between services. It simply only accepted native types The solution was to convert the image to Base64 and traffic it as a string.

  • I was able to solve it, but I need to convert the image to Base64 anyway, I’m testing with Postman, to see if it shows the converted image to Base64. Thanks

1 answer

1


To view the image right in the browser, I was able to solve as follows, I used the function GetInvocationMetaData, have to specify to the browser which return. I will put an example of how I did:

declare in 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;  

Browser other questions tagged

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