httpuploads.dpr 3.2 KB

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