fabriciocolombo / delphi-rest-client-api

A Delphi REST client API to consume REST services written in any programming language.
Apache License 2.0
380 stars 182 forks source link

Download a file with this REST API: help needed #106

Closed Andsup closed 6 years ago

Andsup commented 6 years ago

Hi,

I need some guidance or example to implement with this API a binary file download.

I'm using Delphi 2007 for the client and the server is a PHP script.

Thanks.

rpottsoh commented 6 years ago

This isn't exactly what you are looking for but perhaps it can be of some help anyway. Please check out exercism/windows-installer. The Windows Installer was originally written in C#, last year I rewrote it in Delphi. However, I didn't use the delphi-rest-client-api (I didn't know about it then) and used Delphi Tokyo. This APP determines if the computer has 32 / 64 bit windows and then downloads the appropriate latest version of the Exercism CLI from the github repo that has the CI available for download. The APP is using REST to determine the url of the correct and latest version of the CLI to download.

fernand-o commented 6 years ago

You will need to declare a function to handle the GET response as an stream, ex:

TMyClass = class
  private
    FDestination: string;
    procedure HandleResponse(Response: TStream);
  public
    procedure Download(FileURL, Destination: string);
  end;

...

procedure TMyClass.Download(FileURL, Destination: string);
var
  RestClient: TRestClient;
begin
  RestClient := TRestClient.Create(nil);
  try
    RestClient.ConnectionType := hctWinHttp;

    FDestination := Destination;
    RestClient.Resource(FileURL).Get(HandleResponse);
  finally
    RestClient.Free;
  end;
end;

procedure TMyClass.HandleResponse(Response: TStream);
var
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(FDestination, fmCreate);
  try
    Response.Position := 0;
    FileStream.CopyFrom(Response, Response.Size);
  finally
    FileStream.Free;
  end;
end;
var
  MyClass: TMyClass;
begin
  MyClass := TMyClass.Create;
  MyClass.Download('https://www.cstatic-images.com/car-pictures/main/CAC70AUC171A021001.png', 'c:/car.jpg');
  ...
Andsup commented 6 years ago

HI,

Work perfectly !

Thanks a lot.

André.

De : Fernando Almeida Date : lundi 5 février 2018 13:27 À : fabriciocolombo/delphi-rest-client-api Cc : Andsup ; Author Objet : Re: [fabriciocolombo/delphi-rest-client-api] Download a file with this REST API: help needed (#106)

You will need to declare a function to handle the GET response as an stream, ex:

TMyClass = class private FDestination: string; procedure HandleResponse(Response: TStream); public procedure Download(FileURL, Destination: string); end;

...

procedure TMyClass.Download(FileURL, Destination: string); var RestClient: TRestClient; begin RestClient := TRestClient.Create(nil); try RestClient.ConnectionType := hctWinHttp;

FDestination := Destination;
RestClient.Resource(FileURL).Get(HandleResponse);

finally RestClient.Free; end; end;

procedure TMyClass.HandleResponse(Response: TStream); var FileStream: TFileStream; begin FileStream := TFileStream.Create(FDestination, fmCreate); try Response.Position := 0; FileStream.CopyFrom(Response, Response.Size); finally FileStream.Free; end; end; var MyClass: TMyClass; begin MyClass := TMyClass.Create; MyClass.Download('https://www.cstatic-images.com/car-pictures/main/CAC70AUC171A021001.png', 'c:/car.jpg'); ... — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.