httpuploads.lpr 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 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. {$WARN 5024 OFF}
  28. uses
  29. SysUtils,
  30. BrookUtility,
  31. BrookHTTPUploads,
  32. BrookHTTPRequest,
  33. BrookHTTPResponse,
  34. BrookHTTPServer;
  35. const
  36. PAGE_FORM = Concat(
  37. '<html>',
  38. '<body>',
  39. '<form action="" method="post" enctype="multipart/form-data">',
  40. '<fieldset>',
  41. '<legend>Choose the files:</legend>',
  42. 'File 1: <input type="file" name="file1"/><br>',
  43. 'File 2: <input type="file" name="file2"/><br>',
  44. '<input type="submit"/>',
  45. '</fieldset>',
  46. '</form>',
  47. '</body>',
  48. '</html>'
  49. );
  50. PAGE_DONE = Concat(
  51. '<html>',
  52. '<head>',
  53. '<title>Uploads</title>',
  54. '</head>',
  55. '<body>',
  56. '<strong>Uploaded files:</strong><br>',
  57. '%s',
  58. '</body>',
  59. '</html>'
  60. );
  61. CONTENT_TYPE = 'text/html; charset=utf-8';
  62. type
  63. THTTPServer = class(TBrookHTTPServer)
  64. protected
  65. procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  66. AResponse: TBrookHTTPResponse); override;
  67. end;
  68. procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  69. AResponse: TBrookHTTPResponse);
  70. var
  71. VUpload: TBrookHTTPUpload;
  72. VFile, VList, VError: string;
  73. begin
  74. if ARequest.IsUploading then
  75. begin
  76. VList := '<ol>';
  77. for VUpload in ARequest.Uploads do
  78. if VUpload.Save(False, VError) then
  79. VList := Concat(VList, '<li><a href="?file=', VUpload.Name, '">',
  80. VUpload.Name, '</a></li>')
  81. else
  82. VList := Concat(VList, '<li><font color="red">', VUpload.Name,
  83. ' - failed - ', VError, '</font></li>');
  84. VList := Concat(VList, '</ol>');
  85. AResponse.SendFmt(PAGE_DONE, [VList], CONTENT_TYPE, 200);
  86. end
  87. else
  88. begin
  89. if ARequest.Params.TryValue('file', VFile) then
  90. AResponse.Download(Concat(UploadsDir, PathDelim, VFile))
  91. else
  92. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  93. end;
  94. end;
  95. begin
  96. with THTTPServer.Create(nil) do
  97. try
  98. UploadsDir := Concat(IncludeTrailingPathDelimiter(Sagui.TmpDir), 'uploads');
  99. NoFavicon := True;
  100. Open;
  101. if not Active then
  102. Exit;
  103. ForceDirectories(UploadsDir);
  104. WriteLn('Server running at http://localhost:', Port);
  105. ReadLn;
  106. finally
  107. Free;
  108. end;
  109. end.