Recent

Author Topic: How to download files using just my Lazarus application?  (Read 2319 times)

Jvan

  • Full Member
  • ***
  • Posts: 129
How to download files using just my Lazarus application?
« on: March 05, 2021, 08:24:12 pm »
The server generate a file and a URL to download it. What I need is my Lazarus app downloading that file into a a folder selected by the user.

Thanks.

Gustavo 'Gus' Carreno

  • Sr. Member
  • ****
  • Posts: 369
  • Professional amateur ;-P
Re: How to download files using just my Lazarus application?
« Reply #1 on: March 05, 2021, 08:57:21 pm »
Hi Jvan,

Under the fcl-web package you have have fphttpclient.

There's an example on how to use it on the wiki: fphttpclient

If you don't mind adding third party code, there's also Synapse which you can install via the Online Package Manager

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 892
    • Lebeau Software
Re: How to download files using just my Lazarus application?
« Reply #2 on: March 05, 2021, 08:58:31 pm »
If you don't mind adding third party code, there's also Synapse which you can install via the Online Package Manager

Or Indy, which is also in the OPM, and has an HTTP client.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Gustavo 'Gus' Carreno

  • Sr. Member
  • ****
  • Posts: 369
  • Professional amateur ;-P
Re: How to download files using just my Lazarus application?
« Reply #3 on: March 05, 2021, 09:10:15 pm »
Hi Remy,

Don't ask me why, but I never liked Indy.
I guess I got too used to Synapse and now that FPC has fphhtpclient, I'm investing it it just because you can ditch third party's.

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

GetMem

  • Hero Member
  • *****
  • Posts: 3973
Re: How to download files using just my Lazarus application?
« Reply #4 on: March 05, 2021, 09:55:04 pm »
@gcarreno

Quote
Don't ask me why, but I never liked Indy.
Indy it's a good alternative. I have a client-server application running for years, I never had any issues whatsoever.

Gustavo 'Gus' Carreno

  • Sr. Member
  • ****
  • Posts: 369
  • Professional amateur ;-P
Re: How to download files using just my Lazarus application?
« Reply #5 on: March 05, 2021, 10:10:30 pm »
Hey GetMem,

Indy it's a good alternative. I have a client-server application running for years, I never had any issues whatsoever.

Don't get me wrong, I never said it was bad or inefficient or anything degrading to it's performance.

I was just stating an opinion. And, also, why I left it out. Maybe I shouldn't, but I did and I'm sorry.

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 892
    • Lebeau Software
Re: How to download files using just my Lazarus application?
« Reply #6 on: March 06, 2021, 12:19:06 am »
Don't ask me why, but I never liked Indy.

To each their own.  Some people love it, some people hate it.  It is not for everyone.  But the option is available.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Gustavo 'Gus' Carreno

  • Sr. Member
  • ****
  • Posts: 369
  • Professional amateur ;-P
Re: How to download files using just my Lazarus application?
« Reply #7 on: March 06, 2021, 12:52:03 am »
Hi Remy

But the option is available.

Absolutely and again, totally mea culpa for not mentioning it  :-[

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

Jvan

  • Full Member
  • ***
  • Posts: 129
Re: How to download files using just my Lazarus application?
« Reply #8 on: March 23, 2021, 01:41:33 am »
Hi Remy

But the option is available.

Absolutely and again, totally mea culpa for not mentioning it  :-[

Cheers,
Gus

I have a problem with a exception message.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   fphttpclient,
  10.   fpopenssl,
  11.   openssl;
  12.  
  13. type
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     edtDescargarDe: TEdit;
  20.     edtGuardarEn: TEdit;
  21.     Label1: TLabel;
  22.     Label2: TLabel;
  23.     procedure Button1Click(Sender: TObject);
  24.   private
  25.  
  26.   public
  27.  
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.   Client: TFPHttpClient;
  33.   FS: TStream;
  34.   SL: TStringList;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. var
  44.   vFilename: String;
  45.   vDownloadFrom: String;
  46. begin    
  47.   vFilename := edtGuardarEn.Text;  //'DESCARGAR_FILE.docx'
  48.   vDownloadFrom := edtDescargarDe.Text;
  49.   InitSSLInterface;
  50.   Client := TFPHttpClient.Create(nil);
  51.   FS := TFileStream.Create(vFilename, fmCreate or fmOpenWrite);
  52.   try
  53.     try
  54.       { Allow redirections }
  55.       Client.AllowRedirect := true;
  56.       Client.Get(vDownloadFrom, vFilename);
  57.     except
  58.       on E: EHttpClient do
  59.         ShowMessage(E.Message)
  60.       else
  61.         raise;
  62.     end;
  63.   finally
  64.     FS.Free;
  65.     Client.Free;
  66.     ShowMessage('OK');
  67.   end;
  68. end;
  69.  
  70. end.
  71.  

The file that I wanto to download was created, but it is empty.
« Last Edit: March 23, 2021, 01:43:38 am by Jvan »

lucamar

  • Hero Member
  • *****
  • Posts: 3739
Re: How to download files using just my Lazarus application?
« Reply #9 on: March 23, 2021, 01:48:58 am »
Since it is a docx file, that's probably your antivirus kicking in to check that it has no macro virus and locking it while trying to do it. Though I might be completely wrong ...

Just to make sure: your local server won't be looking for the file at exactly that location, will it?
« Last Edit: March 23, 2021, 01:50:52 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Gustavo 'Gus' Carreno

  • Sr. Member
  • ****
  • Posts: 369
  • Professional amateur ;-P
Re: How to download files using just my Lazarus application?
« Reply #10 on: March 23, 2021, 02:56:31 am »
Hey Jvan,

I have a problem with a exception message.

Ok, first make sure that you have no problem with the Anti-Virus, like @Lucamar said.

Then you'll have to tell me what version of FPC(Specifically FPC) you're using.

There's been some changes from 3.0.4 to 3.2.0 that need to be addressed with some well placed {$IFDEF} mainly because of SLL and the wiki page is not correct. I still need to contribute to that.

I have a working version, both on 3.0.4 and 3.2.0 and with macOS(somebody had some problem on macOS due to library versions changing on Big Sur and I think my code solved it), on a private project that I can share if you want to.

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

GetMem

  • Hero Member
  • *****
  • Posts: 3973
Re: How to download files using just my Lazarus application?
« Reply #11 on: March 23, 2021, 06:27:24 am »
@Jvan
Just replace
Code: Pascal  [Select][+][-]
  1.  Client.Get(vDownloadFrom, vFilename);
with
Code: Pascal  [Select][+][-]
  1. Client.Get(vDownloadFrom, fs);

Gustavo 'Gus' Carreno

  • Sr. Member
  • ****
  • Posts: 369
  • Professional amateur ;-P
Re: How to download files using just my Lazarus application?
« Reply #12 on: March 23, 2021, 07:18:50 am »
Hi GetMem,

@Jvan
Just replace

Yes he does create the TFileStream and never uses it(but Free's it at the end), but the Get() has 2 overloads, one with Filename: String and another with Filestream: TFileStream.
So syntactically and logically the code snippet is correct.
But HAAAA, the file is opened by the TFileStream, Open/Written/Closed by the Get() and then Closed on the FS.Free(). That could mess some of the things!!!

@JVan: Please correct that unfortunate sequence of events, by either removing all code pertaining to FS, or just replace stuff like @GetMem said.
If after that you still have issues, I'll share my code that is safe across FPC 3.0.4/3.2.0.

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

alaa123456789

  • Full Member
  • ***
  • Posts: 167

Handoko

  • Hero Member
  • *****
  • Posts: 4137
  • My goal: build my own game engine using Lazarus
Re: How to download files using just my Lazarus application?
« Reply #14 on: March 23, 2021, 05:56:17 pm »
That's great. Nice to see Lazarus tutorials in videos. I'm waiting for you next videos.

Thank you for creating the video tutorial.

 

TinyPortal © 2005-2018