Recent

Author Topic: I want to make a file downloader, but it keeps encountering issues.  (Read 632 times)

zzzzzzz7

  • New Member
  • *
  • Posts: 18
You like the teacher, I want to make a downloader, but when running inward, it always crashes into the main network base address like @Self before, and it's quite aggressive.



rvk

  • Hero Member
  • *****
  • Posts: 7042
thank you ,But I don't understand. Can you give me the simplest substitute?
You didn't show anything from your code so how can we show a simple substitute?

jamie

  • Hero Member
  • *****
  • Posts: 7761
thank you ,But I don't understand. Can you give me the simplest substitute?
You didn't show anything from your code so how can we show a simple substitute?

Aren't you a mind reader, lost your magic wand? :D

Jamie
The only true wisdom is knowing you know nothing

Aruna

  • Hero Member
  • *****
  • Posts: 807
You like the teacher, I want to make a downloader, but when running inward, it always crashes into the main network base address like @Self before, and it's quite aggressive.
The code below has been tested and works on Linux. Your mileage may vary  :) You must give it the full path including file name to be saved else will choke.

EDIT: The attached ZIP is for you to test and explore.

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, opensslsockets;
  10.  
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Edit1: TEdit;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure DownloadFile(const URL, Dest: string);
  36. var
  37.   F: TFileStream;
  38. begin
  39.   F := TFileStream.Create(Dest, fmCreate);
  40.   try
  41.     TFPHTTPClient.SimpleGet(URL, F);
  42.   finally
  43.     F.Free;
  44.   end;
  45. end;
  46.  
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. begin
  50. //    DownloadFile('https://www.lazarus-ide.org/index.php/', '/home/aruna/debug/download-test.html');
  51.       DownloadFile(Edit1.Text, '/home/aruna/debug/download-test.html');
  52. end;
  53.  
  54. end.
  55.  
« Last Edit: May 26, 2026, 07:43:34 pm by Aruna »

zzzzzzz7

  • New Member
  • *
  • Posts: 18
You like the teacher, I want to make a downloader, but when running inward, it always crashes into the main network base address like @Self before, and it's quite aggressive.
The code below has been tested and works on Linux. Your mileage may vary  :) You must give it the full path including file name to be saved else will choke.

EDIT: The attached ZIP is for you to test and explore.

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, opensslsockets;
  10.  
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Edit1: TEdit;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure DownloadFile(const URL, Dest: string);
  36. var
  37.   F: TFileStream;
  38. begin
  39.   F := TFileStream.Create(Dest, fmCreate);
  40.   try
  41.     TFPHTTPClient.SimpleGet(URL, F);
  42.   finally
  43.     F.Free;
  44.   end;
  45. end;
  46.  
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. begin
  50. //    DownloadFile('https://www.lazarus-ide.org/index.php/', '/home/aruna/debug/download-test.html');
  51.       DownloadFile(Edit1.Text, '/home/aruna/debug/download-test.html');
  52. end;
  53.  
  54. end.
  55.  


//
//Thank you,sir. FileDownLoader.zip(49.85 kB )
//【https://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=74113.0;attach=86744】 but I clicked the button but //nothing happened, no download occurred, nothing happened on Windows 10
//

rvk

  • Hero Member
  • *****
  • Posts: 7042
//
//Thank you,sir. FileDownLoader.zip(49.85 kB )
//【https://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=74113.0;attach=86744】 but I clicked the button but //nothing happened, no download occurred, nothing happened on Windows 10
//
So what file (URL) are you trying to download?

Maybe it's protected somehow (so you can only download it from browsers or it's behind a cloudflare wall or something).

Aruna

  • Hero Member
  • *****
  • Posts: 807
but I clicked the button but //nothing happened, no download occurred, nothing happened on Windows 10

- Did you type in or copy/paste the URL into the TEdit box then click the button?
- What is the URL to the file you are trying to download? Please share this so we can test and verify URL is legit and functional.
- Does the URL open correctly in a web browser?
- Are you testing on localhost, LAN, or internet?
- Is the internet connection working on that machine?
- Where are you trying to save the downloaded file? (The path in my code will not work unless you created this exact same path on your system)
- Does the destination directory exist?

So what you need to do to get this to work:
1. Create a folder where you want to save the downloded URL
2. Add this folder path to the code and replace :
    DownloadFile(Edit1.Text, '/home/aruna/debug/download-test.html'); with
    DownloadFile(Edit1.Text, '/your/file/download/path/download-test.html');

I am using linux so since your using Windows please adjust the path delimiter from '/' to '\'
3. Enter the URL in the TEdit box next to the button
4. Click the button.

Give me till the end of the day I will send you another zip with full error checking and handling hopefully will make things easier for you.


Aruna

  • Hero Member
  • *****
  • Posts: 807
Thank you,sir. FileDownLoader.zip(49.85 kB ) but I clicked the button but //nothing happened, no download occurred, nothing happened on Windows 10
As promised, here is a ZIP file containing a tested, cross-platform unit that is fully working. Screenshot attached.

The unit is preconfigured with a default URL for downloading an image. Simply click the button and the image will be downloaded to the following location:

Windows: C:\Users\<YourUsername>\lazarus-forum-question\ (saved as downloaded.png)
Linux/Unix: /home/<username>/lazarus-forum-question/

For the first test, please verify that the file is correctly downloaded to:
C:\Users\<YourUsername>\lazarus-forum-question\downloaded.png

Once confirmed, you can proceed to the next stage.

Pressing F4 toggles the input mode: it enables the TEdit so the user can enter a custom download URL. Pressing F4 again restores the original preloaded URL.

Handoko

  • Hero Member
  • *****
  • Posts: 5544
  • My goal: build my own game engine using Lazarus
Re: I want to make a file downloader, but it keeps encountering issues.
« Reply #10 on: May 30, 2026, 01:57:13 pm »
~edited~

Aruna's code works. Tested on Linux. But running the code, most likely you will get response 403. Error 403 means the server receive your request but refusing it.
https://en.wikipedia.org/wiki/HTTP_403

For security and resource wasting prevention, many websites do not allow scrappers and bad bots. Aruna's code works correctly if you try to download from:
https://www.blogger.com/img/logo_blogger_40px.png

To make it to be able to download from more websites, you should provide more information in your TFPHTTPClient.Header especially the UserAgent. A visitor without User Agent will be treated as a bad bot.
« Last Edit: May 30, 2026, 02:13:21 pm by Handoko »

 

TinyPortal © 2005-2018