2013-10-29 16:51:16 +01:00
|
|
|
unit FileUploadControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework,
|
2017-01-18 21:53:53 +01:00
|
|
|
MVCFramework.Commons,
|
2013-10-29 16:51:16 +01:00
|
|
|
MVCFramework.Logger;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/file')]
|
|
|
|
TFileUploadController = class(TMVCController)
|
2016-03-23 10:13:33 +01:00
|
|
|
private const
|
|
|
|
UPLOAD_FOLDER = 'uploadedfiles';
|
2013-10-29 16:51:16 +01:00
|
|
|
public
|
2016-03-23 10:13:33 +01:00
|
|
|
[MVCPath]
|
2013-10-29 16:51:16 +01:00
|
|
|
[MVCHTTPMethod([httpPOST])]
|
|
|
|
procedure SaveFile(CTX: TWebContext);
|
2016-03-23 10:13:33 +01:00
|
|
|
[MVCPath('/list')]
|
|
|
|
[MVCProduces('text/html')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure FileList(CTX: TWebContext);
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
system.ioutils,
|
|
|
|
system.Classes,
|
|
|
|
system.SysUtils,
|
2017-01-18 21:53:53 +01:00
|
|
|
system.Types;
|
2013-10-29 16:51:16 +01:00
|
|
|
|
|
|
|
{ TFileUploadController }
|
|
|
|
|
2016-03-23 10:13:33 +01:00
|
|
|
procedure TFileUploadController.FileList(CTX: TWebContext);
|
2013-10-29 16:51:16 +01:00
|
|
|
var
|
|
|
|
UploadedFiles: TStringDynArray;
|
2016-03-23 10:13:33 +01:00
|
|
|
fname: string;
|
|
|
|
begin
|
|
|
|
ResponseStream.AppendLine('<!doctype html><html><body>');
|
|
|
|
ResponseStream.AppendLine('<h2>**Upload Folder Content**</h2>');
|
|
|
|
UploadedFiles := TDirectory.GetFiles(UPLOAD_FOLDER);
|
|
|
|
ResponseStream.AppendLine('<ul>');
|
|
|
|
for fname in UploadedFiles do
|
|
|
|
begin
|
|
|
|
ResponseStream.AppendLine('<li>' + ExtractFileName(fname) + '</li>');
|
|
|
|
end;
|
|
|
|
ResponseStream.AppendLine('</ul>')
|
|
|
|
.AppendLine('<p><a href="/fileupload.html">back to upload form</a></p>')
|
|
|
|
.AppendLine('</body></html>');
|
|
|
|
Render;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TFileUploadController.SaveFile(CTX: TWebContext);
|
|
|
|
var
|
|
|
|
fname: string;
|
|
|
|
I: Integer;
|
|
|
|
fs: TFileStream;
|
2013-10-29 16:51:16 +01:00
|
|
|
begin
|
|
|
|
TDirectory.CreateDirectory(UPLOAD_FOLDER);
|
|
|
|
for I := 0 to CTX.Request.RawWebRequest.Files.Count - 1 do
|
|
|
|
begin
|
2016-03-23 10:13:33 +01:00
|
|
|
fname := String(CTX.Request.Files[I].FileName);
|
|
|
|
fname := TPath.GetFileName(fname.Trim(['"']));
|
|
|
|
if not TPath.HasValidFileNameChars(fname, false) then
|
|
|
|
raise EMVCException.Create
|
|
|
|
(fname + ' is not a valid filename for the hosting OS');
|
2013-10-29 16:51:16 +01:00
|
|
|
Log('Uploading ' + fname);
|
|
|
|
fs := TFile.Create(TPath.Combine(UPLOAD_FOLDER, fname));
|
|
|
|
try
|
|
|
|
fs.CopyFrom(CTX.Request.Files[I].Stream, 0);
|
|
|
|
finally
|
|
|
|
fs.free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-03-23 10:13:33 +01:00
|
|
|
Redirect('/file/list');
|
2013-10-29 16:51:16 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|