I tried using TFileClient in Delphi.

Haruyuki Mohri
2 min readJan 3, 2021

--

There was a file called “System.Net.FileClient.pas” in Delphi 10.3, so I tried using it.Inside this file is a class called TFileClient. Apparently you can access files using this class. To use this class, we need to add some to “uses”.It is below.

System.Net.HttpClient,System.Net.URLClient, System.Net.FileClient, System.Types

This is a simple example using TURLClient.The following is an example of getting a text file on the Internet.

procedure TForm1.Button1Click(Sender: TObject);
begin
var FileClient1:TURLClient := TFileClient.CreateInstance;
var URLResponse1:IURLResponse := FileClient1.Execute('HTTPS', 'https://mjeld.com/index.php',nil,nil);
var str1 := URLResponse1.ContentAsString();
OutputDebugString(PChar(str1));
end;

Since “IURLResponse” has “TStream”, it is also possible to get a binary file.

The following is an example of retrieving a file asynchronously using TFileClient.

procedure TForm1.Button1Click(Sender: TObject);
begin
var FileClient1:TURLClient := TFileClient.CreateInstance;
FileClient1.BeginExecute(hoge,'FILE', 'file:///C:/data/Unit1.pas');
end;

procedure TForm1.hoge(const ASyncResult: IAsyncResult);
begin
if ASyncResult.IsCompleted then
begin
var res1 := TURLClient.EndAsyncURL(ASyncResult);
var s := res1.ContentAsString();
OutputDebugString(PChar(s));
end;

end;

The above code is an example of getting the text of a local file called “Unit1.pas”.Asynchronous processing is possible by using a function called BeginExecute. The hoge method is specified as the first argument, and FILE and the local file (Unit1.pas) are specified as the subsequent arguments. Since Unit1.pas is a text file, it can be converted to a String and output using ContentAsString().

I write the same article in Japanese. It is the following URL.

https://mojelder.hatenablog.com/entry/2015/08/09/011240

--

--

Haruyuki Mohri
Haruyuki Mohri

Written by Haruyuki Mohri

0 Followers

Delphi C++Builder

No responses yet