Recent

Author Topic: File upload  (Read 1445 times)

narag

  • New member
  • *
  • Posts: 8
File upload
« on: July 17, 2024, 11:06:36 pm »
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:

    <form method="post" action="http://ares:55666/" enctype="multipart/form-data">
       <input name="field1" type="text"><br />
       <input type="file">
       <input type="submit" value="enviar">
    </form>

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 := '<html><head><title>test</title></head><body><h1>test</h1></body></html>';
    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

  • Hero Member
  • *****
  • Posts: 1489
Re: File upload
« Reply #1 on: July 18, 2024, 09:06:12 am »
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

  • Hero Member
  • *****
  • Posts: 16158
  • Censorship about opinions does not belong here.
Re: File upload
« Reply #2 on: July 18, 2024, 09:23:12 pm »
OMG the fcl-web package comes with examples. Can you read and compile examples?
Somebody forgot to read the documentation again, not me.
If I smell bad code it usually is bad code and that includes my own code.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8774
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: File upload
« Reply #3 on: July 29, 2024, 04:49:33 am »
Code: Text  [Select][+][-]
  1. ...
  2. <input type="file">
  3. ...
  4.  
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][+][-]
  1. uses
  2.   httpdefs,
  3.   httproute,
  4.   fphttpapp;
  5.  
  6. procedure DisplayUploadForm(AReq: TRequest; AResp: TResponse);
  7. begin
  8.   AResp.Content := '<form method="post" action="/" enctype="multipart/form-data">'
  9.                  + '<input name="field1" type="file">'
  10.                  + '<input type="submit" value="send">'
  11.                  + '</form>'
  12.                  ;
  13. end;
  14.  
  15. procedure HandleUpload(AReq: TRequest; AResp: TResponse);
  16. begin
  17.   if AReq.Files.Count > 0 then begin
  18.     AResp.Contents.LoadFromStream(AReq.Files[0].Stream);
  19.     AResp.Content := '<p>The file you upload contains this:</p><p>' + AResp.Content + '</p>';
  20.   end else begin
  21.     AResp.Content := '<p>No file uploaded</p>';
  22.   end;
  23. end;
  24.  
  25. begin
  26.   HTTPRouter.RegisterRoute('/', rmGet, @DisplayUploadForm);
  27.   HTTPRouter.RegisterRoute('/', rmPost, @HandleUpload);
  28.   Application.Initialize;
  29.   Application.Port := 8789;
  30.   Application.Run;
  31. end.
  32.  

Jorg3000

  • Jr. Member
  • **
  • Posts: 71
Re: File upload
« Reply #4 on: July 29, 2024, 06:26:10 am »
OMG [...] 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?

 

TinyPortal © 2005-2018