Recent

Author Topic: Image source for fpWeb / Brook Framework  (Read 1229 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Image source for fpWeb / Brook Framework
« on: April 11, 2021, 06:26:08 pm »
Platform:  Lazarus 2.0.10 FPC 3.2 running Brook 5 Framework app running on Windows Server 2016

Problem:  I'm "AResponse" -ing Html pages that have image tags.  Those tags require sources.  I can't use:

file:///

because that points to the end user's file system.  So I tried putting the image file in the same folder as the app .exe then just pointing the image tag to the root folder of the app .exe:

 <img src="https://localhost:443/8d00ecb6-f3ba-4120-bef8-116388d4ba01.jpg" alt="Anti-Bot" width="350">

Nope, still no good.

I'm assuming both fpWeb and Brook 5 Framework will use the same solution, since both of them are sending HTML to an end user on a browser that needs image tags to point to something on the server.

Question:

- How can I display images on my end user's browser with the above mentioned server?  (at all)
- How can I display images on my end user's browser with the above mentioned server?  (from a folder of my choosing on my server)

Thanks in advance.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1114
  • Professional amateur ;-P
Re: Image source for fpWeb / Brook Framework
« Reply #1 on: April 11, 2021, 06:42:07 pm »
Hey RedOctober,

From what you're telling us I think you wan to find out how to serve static files.

From a quick Google on "fpweb serve images" I got:
- A link to this forum: https://forum.lazarus.freepascal.org/index.php/topic,45562.0.html
- The wiki entry "fpWeb Tutoruial", section 12.3 (Serving static files): https://wiki.freepascal.org/fpWeb_Tutorial#Serving_Static_Files_.28Embedded_Web_Server.29

Hope that helps you solve your issue.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Image source for fpWeb / Brook Framework
« Reply #2 on: April 11, 2021, 09:25:54 pm »
Can't you put the images alongside the HTML pages and use relative URIs? I mean, can't you use something like (for example):

Code: HTML5  [Select][+][-]
  1.  <img src="images/8d00ecb6-f3ba-4120-bef8-116388d4ba01.jpg" alt="Anti-Bot" width="350">

That would be IMHO an ideal solution.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: Image source for fpWeb / Brook Framework
« Reply #3 on: April 11, 2021, 11:22:32 pm »
Hi lucamar,

Yes, that is what I was thinking too.  Doesn't work that way.  I also tried putting a / in front, like...

<img src="/images/8d00ecb6-f3ba-4120-bef8-116388d4ba01.jpg" alt="Anti-Bot" width="350">

That doesn't work either.  Still stuck. 

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Image source for fpWeb / Brook Framework
« Reply #4 on: April 12, 2021, 12:10:06 am »
Hi lucamar,

Yes, that is what I was thinking too.  Doesn't work that way.  I also tried putting a / in front, like...

<img src="/images/8d00ecb6-f3ba-4120-bef8-116388d4ba01.jpg" alt="Anti-Bot" width="350">

That doesn't work either.  Still stuck.

Putting a slash in front of the URI would only work if the images were in directory "image" directly under  the root of your server.

The question, really, is that if to access your page you have to type something like:
Code: [Select]
http://myserver/scripts/gimmethepage, any relative URI in the generated page will end up having "http://myserver/scripts/" in front, so you have to take that into account to know where to put the images.

Of course, if "scripts" is a virtual directory (i.e. something that gets translated out of the "static" content root folder, as usually happens with e.g. Apache which redirects to a special cgi-bin folder) then a generated (by the browser) URI like: http://myserver/scripts/images/somejpeg.jpg will get interpreted by the server as something like: "look in cgi-bin for the script called 'images' and pass to it a parameter 'somejpeg.jpg'". Imagine the result if there is no "images" script!

What you should probably do is to put the images inside some folder in the static-content section of your server and use a normal URI for them, so if you put a static page "test.html" in the servers root containing:
Code: HTML5  [Select][+][-]
  1.  <img src="http://myserver/images/test.jpg" width="350">
and put 'test.jpg' inside a folder "images" it would work.

Alternatively, you could add a handler for: "http://myserver/scripts/images" and make it return the image file represented by the parameters from wherever it's stored.
« Last Edit: April 12, 2021, 04:09:33 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: Image source for fpWeb / Brook Framework
« Reply #5 on: April 12, 2021, 03:17:42 am »
Problem:  I'm "AResponse" -ing Html pages that have image tags.  Those tags require sources.  I can't use:

Is the HTML returned from your server to the browser dynamically generated by your Brook application or a static HTML file read from disk?

I'm assuming both fpWeb and Brook 5 Framework will use the same solution, since both of them are sending HTML to an end user on a browser that needs image tags to point to something on the server.

For a web server or web application to serve static content, one configuration item is the directory on the server from which the static content is sourced.

For fpWeb, that is the procedure fpwebfile.RegisterFileLocation:

Code: Pascal  [Select][+][-]
  1. // use this to map locations (relative to BaseURL of the application) to physical directories.
  2. // More than one location can be registered. Directory must exist, location must not have / or \
  3. Procedure RegisterFileLocation(Const ALocation,ADirectory : String);

Brook will have its own mechanism. If your Brook application is able to serve static HTML, then it will as easily serve static image files.

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: Image source for fpWeb / Brook Framework
« Reply #6 on: April 15, 2021, 11:46:45 pm »
Hi All, to finish off this thread... I have received instruction from Silvio (Brook 5 Framework author) how to do this.  The code snippet below is not from him, it's from my project.  It just tests to see if the request is for a .jpg filename, and if so, it downloads the file.

Code: Pascal  [Select][+][-]
  1. var idx: Int64;  pth, fnm: String;
  2.  
  3. ...
  4.   idx := High(ARequest.Paths);
  5.   pth := ';' + ARequest.Paths[idx] + ';';
  6. ...
  7.   fnm := StringReplace(pth, ';', '', [rfReplaceAll]);
  8.  
  9.   // Determine if the request is for an image contained in an HTML <img> tag
  10.  
  11.  
  12.  fnm := StringReplace(pth, ';', '', [rfReplaceAll]);
  13.   if Pos(ExtractFileExt(fnm), ';.jpg;.png;.bmp;') > 0 then
  14.     begin
  15.       fnm := (your path to your images on your hard drive) + '/' + fnm;
  16.       AResponse.Download(fnm);
  17.       Exit;
  18.     end;
  19.  
  20.   brk_rtr.Route(ASender, ARequest, AResponse);
  21.  

The image tag scr= attribute value has to be a full URL similar to below:

<img src="https://localhost:443/imagefolder/myimage.jpg"   ...


My project is now back on track and images are showing up in the web pages that call for them.

Thanks to all who made suggestions on how to fix.
« Last Edit: April 15, 2021, 11:53:31 pm by RedOctober »

 

TinyPortal © 2005-2018