delphimvcframework/samples/fileupload/FileUploadControllerU.pas

130 lines
3.0 KiB
ObjectPascal
Raw Normal View History

2013-10-29 16:51:16 +01:00
unit FileUploadControllerU;
interface
uses
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,
MVCFramework.Commons,
2013-10-29 16:51:16 +01:00
MVCFramework.Logger;
type
[MVCPath]
2013-10-29 16:51:16 +01:00
TFileUploadController = class(TMVCController)
2024-04-10 17:12:42 +02:00
public const
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
[MVCPath('/')]
[MVCHTTPMethod([httpGET])]
2024-04-10 17:12:42 +02:00
function Index: string;
[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>;
lFName: string;
begin
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;
end;
end;
2024-04-10 17:12:42 +02:00
function TFileUploadController.Index: String;
var
lFileNames: TList<String>;
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;
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;
var
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.