2013-10-29 16:51:16 +01:00
|
|
|
unit FileUploadControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2019-02-01 20:10:51 +01:00
|
|
|
ReqMulti, // this unit is required to enable file uploading
|
2024-04-10 17:12:42 +02:00
|
|
|
System.Generics.Collections,
|
2013-10-29 16:51:16 +01:00
|
|
|
MVCFramework,
|
2017-01-18 21:53:53 +01:00
|
|
|
MVCFramework.Commons,
|
2013-10-29 16:51:16 +01:00
|
|
|
MVCFramework.Logger;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
2019-02-01 20:10:51 +01:00
|
|
|
[MVCPath]
|
2013-10-29 16:51:16 +01:00
|
|
|
TFileUploadController = class(TMVCController)
|
2024-04-10 17:12:42 +02:00
|
|
|
public const
|
2016-03-23 10:13:33 +01:00
|
|
|
UPLOAD_FOLDER = 'uploadedfiles';
|
2024-04-10 17:12:42 +02:00
|
|
|
private
|
|
|
|
function GetFiles: TList<String>;
|
|
|
|
|
|
|
|
protected
|
|
|
|
procedure OnBeforeAction(AContext: TWebContext; const AActionName: string; var AHandled: Boolean); override;
|
2013-10-29 16:51:16 +01:00
|
|
|
public
|
2019-02-01 20:10:51 +01:00
|
|
|
[MVCPath('/')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2024-04-10 17:12:42 +02:00
|
|
|
function Index: string;
|
2019-02-01 20:10:51 +01:00
|
|
|
|
|
|
|
[MVCPath('/file/upload')]
|
2013-10-29 16:51:16 +01:00
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2024-04-10 17:12:42 +02:00
|
|
|
function SaveFile: IMVCResponse;
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
system.ioutils,
|
|
|
|
system.Classes,
|
|
|
|
system.SysUtils,
|
2024-04-10 17:12:42 +02:00
|
|
|
system.Types,
|
|
|
|
JsonDataObjects;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
{ TFileUploadController }
|
|
|
|
|
2024-04-10 17:12:42 +02:00
|
|
|
//function TFileUploadController.FileList: String;
|
|
|
|
//var
|
|
|
|
// lFileNames: TList<String>;
|
|
|
|
//begin
|
|
|
|
// lFileNames := GetFiles;
|
|
|
|
// try
|
|
|
|
// ViewData['files'] := lFileNames;
|
|
|
|
// Result := PageFragment(['filelist']);
|
|
|
|
// finally
|
|
|
|
// lFileNames.free;
|
|
|
|
// end;
|
|
|
|
//end;
|
|
|
|
|
|
|
|
function TFileUploadController.GetFiles: TList<String>;
|
2013-10-29 16:51:16 +01:00
|
|
|
var
|
2024-04-10 17:12:42 +02:00
|
|
|
lUploadedFiles: TArray<string>;
|
2019-02-01 20:10:51 +01:00
|
|
|
lFName: string;
|
2016-03-23 10:13:33 +01:00
|
|
|
begin
|
2019-02-01 20:10:51 +01:00
|
|
|
lUploadedFiles := TDirectory.GetFiles(UPLOAD_FOLDER);
|
2024-04-10 17:12:42 +02:00
|
|
|
Result := TList<String>.Create;
|
|
|
|
try
|
|
|
|
for lFName in lUploadedFiles do
|
|
|
|
begin
|
|
|
|
Result.Add(ExtractFileName(lFName));
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
2016-03-23 10:13:33 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2024-04-10 17:12:42 +02:00
|
|
|
function TFileUploadController.Index: String;
|
|
|
|
var
|
|
|
|
lFileNames: TList<String>;
|
2019-02-01 20:10:51 +01:00
|
|
|
begin
|
2024-04-10 17:12:42 +02:00
|
|
|
lFileNames := GetFiles;
|
|
|
|
try
|
|
|
|
ViewData['files'] := lFileNames;
|
|
|
|
ViewData['files_count'] := lFileNames.Count;
|
|
|
|
Result := Page(['fileupload','filelist']);
|
|
|
|
finally
|
|
|
|
lFileNames.free;
|
|
|
|
end;
|
2019-02-01 20:10:51 +01:00
|
|
|
end;
|
|
|
|
|
2024-04-10 17:12:42 +02:00
|
|
|
procedure TFileUploadController.OnBeforeAction(AContext: TWebContext;
|
|
|
|
const AActionName: string; var AHandled: Boolean);
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
SetPagesCommonHeaders(['header']);
|
|
|
|
SetPagesCommonFooters(['footer']);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TFileUploadController.SaveFile: IMVCResponse;
|
2016-03-23 10:13:33 +01:00
|
|
|
var
|
2019-02-01 20:10:51 +01:00
|
|
|
lFName: string;
|
|
|
|
lFile: TFileStream;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2024-04-10 17:12:42 +02:00
|
|
|
if Context.Request.RawWebRequest.Files.Count <> 1 then
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
2024-04-10 17:12:42 +02:00
|
|
|
Exit(RedirectResponse('/'));
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
2024-04-10 17:12:42 +02:00
|
|
|
lFName := String(Context.Request.Files[0].FileName);
|
|
|
|
lFName := TPath.GetFileName(lFName.Trim(['"']));
|
|
|
|
if not TPath.HasValidFileNameChars(lFName, false) then
|
|
|
|
begin
|
|
|
|
raise EMVCException.Create
|
|
|
|
(HTTP_STATUS.BadRequest, lFName + ' is not a valid filename for the hosting OS');
|
|
|
|
end;
|
|
|
|
if TFile.Exists(TPath.Combine(UPLOAD_FOLDER, lFName)) then
|
|
|
|
begin
|
|
|
|
raise EMVCException.Create
|
|
|
|
(HTTP_STATUS.BadRequest, lFName + ' already present, cannot override');
|
|
|
|
end;
|
|
|
|
Log('Uploading ' + lFName);
|
|
|
|
lFile := TFile.Create(TPath.Combine(UPLOAD_FOLDER, lFName));
|
|
|
|
try
|
|
|
|
lFile.CopyFrom(Context.Request.Files[0].Stream, 0);
|
|
|
|
finally
|
|
|
|
lFile.free;
|
|
|
|
end;
|
|
|
|
Result := RedirectResponse('/');
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|