DMClient.pas 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. unit DMClient;
  26. {$MODE DELPHI}
  27. interface
  28. uses
  29. DB,
  30. Classes,
  31. BufDataset,
  32. FPHTTPClient;
  33. type
  34. TClient = class(TDataModule)
  35. BufDataset: TBufDataset;
  36. public
  37. procedure LoadPersons(const AURL: string);
  38. procedure SavePersons(const AURL: string);
  39. end;
  40. var
  41. Client: TClient;
  42. implementation
  43. {$R *.lfm}
  44. procedure TClient.LoadPersons(const AURL: string);
  45. var
  46. VData: TStream;
  47. begin
  48. VData := TBytesStream.Create;
  49. try
  50. TFPHTTPClient.SimpleGet(AURL, VData);
  51. VData.Seek(0, TSeekOrigin.soBeginning);
  52. BufDataset.Close;
  53. BufDataset.LoadFromStream(VData, dfBinary);
  54. finally
  55. VData.Free;
  56. end;
  57. end;
  58. procedure TClient.SavePersons(const AURL: string);
  59. var
  60. VClient: TFPHTTPClient;
  61. begin
  62. if BufDataset.State in dsEditModes then
  63. BufDataset.Post;
  64. VClient := TFPHTTPClient.Create(nil);
  65. VClient.RequestBody := TBytesStream.Create;
  66. try
  67. BufDataset.SaveToStream(VClient.RequestBody, dfBinary);
  68. VClient.RequestBody.Seek(0, TSeekOrigin.soBeginning);
  69. VClient.Post(AURL);
  70. finally
  71. VClient.RequestBody.Free;
  72. VClient.Free;
  73. end;
  74. end;
  75. end.