File upload lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- narag · 2024-07-17 21:06 Hello, I want to write a very simple server that will serve a couple of pages, one that receives an uploaded file from an HTML form, just like this:    
       
                   
Also the other page will offer the received file and other simple pages for download. No need for https, the server will be deployed behind Apache, that will take care of that layer. So I see that there're units in the FCL that seem to cover this functionality: fphttpserver and httpdefs. I wrote a handler for the OnRequest event like this:    Log(ARequest.DefaultRequestUploadDir);    Log('Uploaded files: ' + IntToStr(ARequest.Files.Count));     if ARequest.Files.Count > 0 then         Log(ARequest.Files[0].FileName);     AResponse.Content := 'test

test

';     AResponse.SendResponse;  The response gets sent fine, but the log shows 0 files and an empty default dir. I guess that I can figure out the code (tracing it) but it would be a lot better if some kind soul could provide me with a minimal example that shows the file upload in action. -- TIA,    Nico ---------------------------------------------------------------------- egsuh · 2024-07-18 07:06 I have experienced the same problem and solved it, but do not remember exactly what were the causes. I remember one situation --- when I rebooted the server. The files sent were stored as CGInnnnn.TMP where nnnnn are serial numbers from 1 with increasing by 1. When I reboot the server, the CGI server seems to start with CGI00001.TMP again. In this case, there exist the file already, and Files.count were 0. Temp directory was c:\windows\temp in Windows case. ---------------------------------------------------------------------- Thaddy · 2024-07-18 19:23 OMG the fcl-web package comes with examples. Can you read and compile examples? Somebody forgot to read the documentation again, not me. ---------------------------------------------------------------------- Leledumbo · 2024-07-29 02:49 Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...... By HTML standards, input fields with no name will not be sent as part of the request. Here's a fixed example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  httpdefs,  httproute,  fphttpapp; procedure DisplayUploadForm(AReq: TRequest; AResp: TResponse);begin  AResp.Content := '
'               + ''                 + ''                 + '
'               ;end; procedure HandleUpload(AReq: TRequest; AResp: TResponse);begin  if AReq.Files.Count > 0 then begin    AResp.Contents.LoadFromStream(AReq.Files[0].Stream);    AResp.Content := '

The file you upload contains this:

' + AResp.Content + '

';  end else begin    AResp.Content := '

No file uploaded

';  end;end; begin  HTTPRouter.RegisterRoute('/', rmGet, @DisplayUploadForm);  HTTPRouter.RegisterRoute('/', rmPost, @HandleUpload);  Application.Initialize;  Application.Port := 8789;  Application.Run;end. ---------------------------------------------------------------------- Jorg3000 · 2024-07-29 04:26 Quote from: Thaddy on July 18, 2024, 09:23:12 pmOMG [...] Can you read and compile examples? Somebody forgot to read the documentation again [...] Did Thaddy actually make up a large portion of his 15,000 comments by ranting against posts and other users? Thaddy, have you ever thought about just not writing if you don't want to help?