Forum > Networking and Web Programming
[SOLVED] webserver does not receive files
egsuh:
I was replying to questions on webserver related questions, but ironically I cannot make my own webserver to receive files. I think I had similar experience in the past, but cannot remember how I solved it.
Server-side code is as following. It will display the local file names.
--- 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";}};} ---procedure TwmWebTests.upfileRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);var i: integer; s: string;begin s := Format('Files uploaded (count=%d):<br>', [ARequest.Files.Count]); for i := 0 to ARequest.Files.Count-1 do begin s += ARequest.Files[i].LocalFileName + '<br>'; end; AResponse.Content := s; Handled:= True;end;
And html file that sends file to the server is :
--- 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";}};} ---<!Doctype html><html><head></head><body> <form method="post" action="http://localhost/myapp/upfile" target="_blank">File Upload <input type="file" name="files"><br> <input type="submit"></form> </body></html>
When I select a file and submit from the browser, it displays following. So, there are no problem in setting up webserver itself.
--- Quote --- Files Uploaded (count=0):
--- End quote ---
Server is Windows Server 2019 Standard.
Leledumbo:
--- Quote from: egsuh on December 04, 2022, 03:04:16 pm ---<form method="post" action="http://localhost/myapp/upfile" target="_blank">File Upload
--- End quote ---
File upload requires:
--- 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";}};} ---enctype="multipart/form-data" in the form attribute, cannot be with the default "application/x-www-form-urlencoded".
egsuh:
Wow, now it works. Following is displayed on submission.
--- Quote ---Files uploaded (count=1):
C:\Windows\TEMP\CGI00016.tmp
--- End quote ---
There are many web pages that explain how to write HTML for file uploads, but I couldn't find any example that specified enctype.
Leledumbo:
--- Quote from: egsuh on December 05, 2022, 06:27:44 am ---There are many web pages that explain how to write HTML for file uploads, but I couldn't find any example that specified enctype.
--- End quote ---
No idea with that, when I first learned it back about 15 years ago, all tutorials specifically mention it. I guess today people don't really write HTML by hand and forget about this detail? I really don't know. But I do know many frameworks provide a function that only needs to be passed any element, typically div, then it magically turns into a file upload, hiding it from the programmer.
krolikbest:
Hi,
to avoid create new thread I linked my question there. So simply code:
--- 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";}};} ---program project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} sysutils, fphttpapp, HTTPDefs, fpwebfile, httproute; procedure Test(aReq: TRequest; aRes: TResponse);var n: Integer; f: TUploadedFile; i: Integer; s : string; begin n := AReq.Files.Count; if n = 0 then begin aRes.Code:=200; aRes.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'fileUploader.html'); end else begin f := AReq.Files[0]; f.LocalFileName:=ExtractFilePath(ParamStr(0)); //apparently this does not set a default folder s:=f.LocalFileName; ARes.Code:=200; ARes.Content := s; end;end; begin Application.Port:=8080; HTTPRouter.RegisterRoute('/',@Test, true); Application.threaded := true; Application.Initialize; Application.Run;end. It works but question is how can I set a specified folder on server side for uploaded files? Using LocalFileName as I did seems to be incorrect..
Regards,
Navigation
[0] Message Index
[#] Next page