-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuDM.pas
105 lines (93 loc) · 2.82 KB
/
uDM.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
unit uDM;
interface
uses
System.SysUtils, System.Classes, REST.Types, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet,
FireDAC.Comp.Client, REST.Response.Adapter, REST.Client, Data.Bind.Components,
Data.Bind.ObjectScope;
type
TDM = class(TDataModule)
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter;
FDMemTable1: TFDMemTable;
RESTClient2: TRESTClient;
RESTRequest2: TRESTRequest;
RESTResponse2: TRESTResponse;
RESTResponseDataSetAdapter2: TRESTResponseDataSetAdapter;
FDMemTable2: TFDMemTable;
RESTClient3: TRESTClient;
RESTRequest3: TRESTRequest;
RESTResponse3: TRESTResponse;
RESTResponseDataSetAdapter3: TRESTResponseDataSetAdapter;
FDMemTable3: TFDMemTable;
RESTClient4: TRESTClient;
RESTRequest4: TRESTRequest;
RESTResponse4: TRESTResponse;
RESTResponseDataSetAdapter4: TRESTResponseDataSetAdapter;
FDMemTable4: TFDMemTable;
private
{ Private declarations }
public
{ Public declarations }
function FileToBase64(const FileName: string): string;
function MemoryStreamToBase64(const MemoryStream: TMemoryStream): string;
end;
var
DM: TDM;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
uses
System.NetEncoding, System.Net.Mime;
function TDM.MemoryStreamToBase64(const MemoryStream: TMemoryStream): string;
var
OutputStringStream: TStringStream;
Base64Encoder: TBase64Encoding;
MimeType: string;
begin
MemoryStream.Position := 0;
OutputStringStream := TStringStream.Create('', TEncoding.ASCII);
try
Base64Encoder := TBase64Encoding.Create;
try
Base64Encoder.Encode(MemoryStream, OutputStringStream);
MimeType := 'image/png';
Result := 'data:' + MimeType + ';base64,' + OutputStringStream.DataString;
finally
Base64Encoder.Free;
end;
finally
OutputStringStream.Free;
end;
end;
function TDM.FileToBase64(const FileName: string): string;
var
InputStream: TFileStream;
OutputStringStream: TStringStream;
Base64Encoder: TBase64Encoding;
MimeType: string;
LKind: TMimeTypes.TKind;
begin
InputStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
OutputStringStream := TStringStream.Create('', TEncoding.ASCII);
try
Base64Encoder := TBase64Encoding.Create;
try
Base64Encoder.Encode(InputStream, OutputStringStream);
TMimeTypes.Default.GetFileInfo(FileName,MimeType,LKind);
Result := 'data:' + MimeType + ';base64,' + OutputStringStream.DataString;
finally
Base64Encoder.Free;
end;
finally
OutputStringStream.Free;
end;
finally
InputStream.Free;
end;
end;
end.