Recent

Author Topic: (SOLVED) fp-web trick and help  (Read 2364 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
(SOLVED) fp-web trick and help
« on: November 16, 2018, 08:39:01 am »
Good morning, I created a web project using the fazar-lazarus components. You can find the project here:

www.lazaruspascal.it/esempi/test_web_app.zip

My intent is to keep HTML files out of the executable.

In the past I had already tried to use the AdmLte template to a lazarus web project but without success, because once the html page was called, all the css, js files contained in the HTML file were not returned. Now with a little trick I can get all the files back from my embedded web server, but the result to the browser is not correct. Who tells me which detail escapes me?

The comments in the executable are in Italian, but the source is really simple, there are a few lines of code.

Then in unit1 in the DataModuleRequest (here you have to change the value of the url: = 'your_path/test_web_app' + url; with the path of your operating system) you will find the heart of my reasoning.

Thanks to anyone who wants to give me a hand
« Last Edit: November 16, 2018, 01:42:12 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: fp-web trick and help
« Reply #1 on: November 16, 2018, 01:41:44 pm »
Solved with this code:

Code: Pascal  [Select][+][-]
  1. procedure TTFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2.   AResponse: TResponse; Var Handled: Boolean);
  3. var
  4.   url: string;
  5.   fine_nome_file: integer;
  6.   f: TFileStream;
  7.   estensione: string;
  8.   ret: boolean;
  9. begin
  10.  
  11.      ret:=false;
  12.  
  13.      url := stringReplace(ARequest.URL, '/', System.DirectorySeparator, [RfReplaceAll]);
  14.  
  15.      if trim(url)<>'' then
  16.      begin
  17.        url := '/Users/francesco/Documents/sw/cgi2test' + url;
  18.        fine_nome_file:=Pos('?',url);
  19.        if fine_nome_file>0 then
  20.        begin
  21.             url:=Copy(url,1,fine_nome_file-1);
  22.        end;
  23.      end;
  24.  
  25.      if FileExists(url) then
  26.      begin
  27.          writeln('trovato:' + url);
  28.          f := TFileStream.Create(url,fmOpenRead);
  29.          try
  30.            AResponse.Code:=200;
  31.            AResponse.FreeContentStream := False;
  32.  
  33.            estensione:=ExtractFileExt(url);
  34.  
  35.            if uppercase(estensione) = '.CSS' then
  36.               AResponse.ContentType := 'text/css;charset=utf-8'
  37.            else
  38.                AResponse.ContentType := 'text/html;charset=utf-8'; // 'application/octet-stream'; //<--questo commentato serve per mandare file al browser
  39.            AResponse.ContentStream := f;
  40.            AResponse.SendContent;
  41.            ret:=true;
  42.          finally
  43.            f.Free;
  44.          end;
  45.      end else begin
  46.          writeln('non trovato:' + url);
  47.      end;
  48.      ret:=true;
  49.      Handled:=ret;
  50. end;
  51.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018