Forum > Networking and Web Programming

File upload

(1/1)

narag:
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:
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:
OMG the fcl-web package comes with examples. Can you read and compile examples?
Somebody forgot to read the documentation again, not me.

Leledumbo:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...<input type="file">... By HTML standards, input fields with no name will not be sent as part of the request. Here's a fixed example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  httpdefs,  httproute,  fphttpapp; procedure DisplayUploadForm(AReq: TRequest; AResp: TResponse);begin  AResp.Content := '<form method="post" action="/" enctype="multipart/form-data">'                 + '<input name="field1" type="file">'                 + '<input type="submit" value="send">'                 + '</form>'                 ;end; procedure HandleUpload(AReq: TRequest; AResp: TResponse);begin  if AReq.Files.Count > 0 then begin    AResp.Contents.LoadFromStream(AReq.Files[0].Stream);    AResp.Content := '<p>The file you upload contains this:</p><p>' + AResp.Content + '</p>';  end else begin    AResp.Content := '<p>No file uploaded</p>';  end;end; begin  HTTPRouter.RegisterRoute('/', rmGet, @DisplayUploadForm);  HTTPRouter.RegisterRoute('/', rmPost, @HandleUpload);  Application.Initialize;  Application.Port := 8789;  Application.Run;end. 

Jorg3000:

--- Quote from: Thaddy on July 18, 2024, 09:23:12 pm ---OMG [...] Can you read and compile examples? Somebody forgot to read the documentation again [...]
--- End quote ---

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?

Navigation

[0] Message Index

Go to full version