Recent

Author Topic: FPHttpServer provide .png files  (Read 908 times)

anderbelluno

  • New Member
  • *
  • Posts: 44
FPHttpServer provide .png files
« on: April 07, 2023, 03:56:36 pm »
Hey guys.
I need to provide .png files using FPHTTPServer, I can't find much on the subject.
I'm studying the possibilities, to later improve the codes.
Note: these are tests.

Code: Pascal  [Select][+][-]
  1. class procedure TLib.LoadImg(var aStream: TMemoryStream);
  2. var
  3.    xFile : TMemoryStream;
  4. begin
  5.    xFile := nil;
  6.    xFile := TMemoryStream.Create;
  7.    try
  8.      xFile.LoadFromFile('C:\Users\anderson\Desktop\Lazarus\FPHTTP\Server\img.png');
  9.      xFile.Position:=0;
  10.      xFile.SaveToStream(aStream);
  11.    finally
  12.      FreeAndNil(xFile);
  13.    end;
  14.  
  15. end;

This procedure reads the file...


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FPHttpServer1Request(Sender: TObject;
  2.    var ARequest: TFPHTTPConnectionRequest;
  3.    var AResponse: TFPHTTPConnectionResponse);
  4. var
  5.    xStream: TMemoryStream;
  6. begin
  7.        xStream := TMemoryStream.Create;
  8.        try
  9.         TLib.LoadImg(xStream);
  10.         xStream.Position:= 0;
  11.         xStream.SaveToStream(AResponse.ContentStream);
  12.         AResponse.ContentType:='image/png'; //; charset=utf-8
  13.        finally
  14.          FreeAndNil(xStream);
  15.        end;
  16. end;

This is the FPHTTPServer's onRequest.

But the image that is returned is not correct.

What am I doing wrong?

Thanks.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPHttpServer provide .png files
« Reply #1 on: April 07, 2023, 05:32:00 pm »
Though I don't know why you want to reinvent the wheel, you can take a look at how it's done.

anderbelluno

  • New Member
  • *
  • Posts: 44
Re: FPHttpServer provide .png files
« Reply #2 on: April 07, 2023, 10:05:58 pm »
Excuse my ignorance my friend, I never wanted to reinvent the wheel, I'm simply learning to use the component, so I'm making mistakes and learning.
Thanks a lot for your help, it worked great.

 

TinyPortal © 2005-2018