httpuploads.lpr 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program httpuploads;
  26. {$MODE DELPHI}
  27. uses
  28. SysUtils,
  29. BrookUtility,
  30. BrookHTTPUploads,
  31. BrookHTTPRequest,
  32. BrookHTTPResponse,
  33. BrookHTTPServer;
  34. const
  35. PAGE_FORM = Concat(
  36. '<html>',
  37. '<body>',
  38. '<form action="" method="post" enctype="multipart/form-data">',
  39. '<fieldset>',
  40. '<legend>Choose the files:</legend>',
  41. 'File 1: <input type="file" name="file1"/><br>',
  42. 'File 2: <input type="file" name="file2"/><br>',
  43. '<input type="submit"/>',
  44. '</fieldset>',
  45. '</form>',
  46. '</body>',
  47. '</html>'
  48. );
  49. PAGE_DONE = Concat(
  50. '<html>',
  51. '<head>',
  52. '<title>Uploads</title>',
  53. '</head>',
  54. '<body>',
  55. '<strong>Uploaded files:</strong><br>',
  56. '%s',
  57. '</body>',
  58. '</html>'
  59. );
  60. CONTENT_TYPE = 'text/html; charset=utf-8';
  61. type
  62. THTTPServer = class(TBrookHTTPServer)
  63. protected
  64. procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  65. AResponse: TBrookHTTPResponse); override;
  66. end;
  67. procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  68. AResponse: TBrookHTTPResponse);
  69. var
  70. VUpload: TBrookHTTPUpload;
  71. VFile, VList, VError: string;
  72. begin
  73. if ARequest.IsUploading then
  74. begin
  75. VList := '<ol>';
  76. for VUpload in ARequest.Uploads do
  77. if VUpload.Save(False, VError) then
  78. VList := Concat(VList, '<li><a href="?file=', VUpload.Name, '">',
  79. VUpload.Name, '</a></li>')
  80. else
  81. VList := Concat(VList, '<li><font color="red">', VUpload.Name,
  82. ' - failed - ', VError, '</font></li>');
  83. VList := Concat(VList, '</ol>');
  84. AResponse.SendFmt(PAGE_DONE, [VList], CONTENT_TYPE, 200);
  85. end
  86. else
  87. begin
  88. if ARequest.Params.TryValue('file', VFile) then
  89. AResponse.Download(Concat(UploadsDir, PathDelim, VFile))
  90. else
  91. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  92. end;
  93. end;
  94. begin
  95. with THTTPServer.Create(nil) do
  96. try
  97. UploadsDir := Concat(IncludeTrailingPathDelimiter(Sagui.TmpDir), 'uploads');
  98. NoFavicon := True;
  99. Open;
  100. if not Active then
  101. Exit;
  102. ForceDirectories(UploadsDir);
  103. WriteLn('Server running at http://localhost:', Port);
  104. ReadLn;
  105. finally
  106. Free;
  107. end;
  108. end.