[solved] How to download a file from the internet by code? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Nicole · 2022-08-11 15:24 I have a link to an pdf file in the internet. It is posted every day new. I need this in my software. Not sure, but I think with TLS 1.3 on side of the server. How can I download the file? In Delphi it was very hard and ceased working after the update from 1.2. to 1.3. (not exactly sure about the version) ---------------------------------------------------------------------- Handoko · 2022-08-11 15:32 You can download the demo "File Download" in the Networking category in the link below: https://wiki.freepascal.org/Portal:HowTo_Demos#Networking ---------------------------------------------------------------------- wp · 2022-08-11 16:17 Sorry this is a very advanced sample, not good for a beginner. For a beginner I'd recommend the attached simple code which works for most standard downloads on Windows and Linux (probably also on mac, not tested). ---------------------------------------------------------------------- alaa123456789 · 2022-08-11 18:43 hope this help https://youtu.be/O4Mj76O1-SQ ---------------------------------------------------------------------- Nicole · 2022-08-12 08:19 @wp: Thank you so much for this project! It worked at sudden,- although not (now) for me: Perhaps about the history: I could download a pdf-file by Delphi as file. Then I used by Delphi a command prompt pdf converter which transformed it to ASCII. This ASCII I read by Delphi "by foot" and hoped, the layout will never change. Under the line I had no work with it, everything was done in the background of my software. So the first step is done, I changed the link-address clicked at the button and the memo is filled. Unfortunately, the memo makes no sense. There are 2 ways out: Either I do save the download link into a file, not into a stream. Or what I would prefer (because it would make me independant of this command line tool software): there is a way to convert the pdf info ASCII by Lazarus. There shall be a pdf-tool somewhere, but I am not sure about the use and which is best. So this is, what the project one gives me, when I try to grab my file: %PDF-1.3 1 0 obj [/PDF /Text /ImageB /ImageC /ImageI] endobj 7 0 obj << /Length 10707 /Filter /FlateDecode >> stream x?}ko?H??????;ئ?~, ?v?nYrK??????R??ܳ?~3?,f?±?ڢ??)?8L?U?????r ȋ?k?6??"?Ϳ7?????T?u]gE?5??Y???/?5??.+?1o?l󡱠?6?Ua_`~????1??^|_g C>?cv?9+?? uS?ySe7???۪???/?????}???n?3??k???~??c?۲?Mg??}?L{?4??}, ?!??*?????>{????9??7yE?ؚ7m????ھ@???%????6????v?}??1??????"ļ?Ð?/?Ȫj?e ?? v?y?2o?*x?J??Z+??x??CUW]??U׭}m\?@#??|n@??yG??`E?6?}yS?M?My??d* ??Φ?8?5?#?L??K//o??W?no׿0) Then site:=Chr(34)+site+Chr(34);  shell (@f[1]); if fileexists(newfile) then loadfile(content,newfile);  If (Length(content)<>0) Then     Shell(pchar( Chr(34) +GetCurrentDir +  '\'+newfile + Chr(34)));  end; varc:ansistring= 'https://promo.betfai r.com/betfairsp/prices/dwbfpricesukwin13072020.csv'; begindownload(c,' New.csv');writeln('press return to finish . . .');readln;end.  Tested Win 10 here. Good luck. ---------------------------------------------------------------------- BobDog · 2022-08-13 14:14 Here is another Windows method As before the test site is for a csv file, and the downloaded file is New.csv in the current directory. (you should change to your site and maybe use New.pdf or something similar) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   usessysutils;  function  shell(s:pchar):integer ; cdecl external 'msvcrt.dll' name 'system'; function filelength(filename:ansistring):longword;Var F : File of byte;var L:longword;beginAssign (F,filename);  Reset (F);  L:=FileSize(F);  Close (F);  exit(L);end;  procedure loadfile(var content: ansistring;filename:ansistring);   Var Fin : File;   Var x:int32;   begin   x:=filelength(filename);   setlength(content,x);   Assign (Fin,filename);   Reset (Fin,x);   BlockRead (Fin,content[1],1);   close(fin);end;  procedure download(site:ansiString;newfile:ansistrin g);varf,content:ansistring;begincontent:='';if fileexists(newfile) then deletefile(newfile);  If (pos(' ',site) <>0) Then site:=Chr(34)+site+Chr(34);  f:='type nul > ' +GetCurrentDir +'\'+newfile; // create empty file  shell (pchar(f));  shell('bitsadmin /reset');  shell ('bitsadmin /create mydownload');  f:='bitsadmin  /transfer mydownload  /download  /priority normal /DYNAMIC ' +  site +' '+ Chr(34) + GetCurrentDir +'\'+ newfile +Chr(34);  Shell(pchar(f));   shell ('bitsadmin /complete mydownload');  if (filelength(newfile)<>0) then loadfile(content,newfile) else exit;  f:=Chr(34) +GetCurrentDir +  '\'+newfile + Chr(34);  If (Length(content)>4) Then     Shell (pchar(f));End;  varsite:ansistring= 'https://promo.betfair.com/betfai rsp/prices/dwbfpricesukwin13072020.csv'; beginshell('Title Please wait a few seconds until Job completed');download(site,'New.csv');writeln('Press return to finish . . .');readln;end. ---------------------------------------------------------------------- Nicole · 2022-08-16 19:35 Thank you for the answer. I read it several times, because I am new to Lazarus. Where can I give my pdf file as input? The file I download is a pdf. ---------------------------------------------------------------------- BobDog · 2022-08-17 04:40 I tried the site given by wp, using the bitsadmin method. site given is 'https://www.ietf.org/rfc/rfc2445.txt' so download(site,'New.txt'); and windows opened this text file via notepad i.e. Shell (pchar(f)) is the command to open the file at the end of the procedure. If your download site is for  .pdf then download(site,'New.pdf'); and windows should open the New.pdf file with your .pdf reader. That's all my code does. from pdf to ascii is another story. Please note my methods are just pascal, and not based on the lazarus ide, I use Geany as an ide/editor. But the code should run in the Lazarus ide as a simple program. ---------------------------------------------------------------------- Nicole · 2022-08-17 06:46 So I thank you both for this very helpful answers and make the thread as solved. The download of the project is highly recommended! ---------------------------------------------------------------------- wp · 2022-08-17 17:58 To complete my reply #2 with respect to downloading pdf files into a memo I am attaching a simple solution which delegates the extraction of the text from the pdf file to the tool "pdftotext.exe" (https://www.xpdfreader.com/pdftotext-man.html, binary for Windows included in the attachment).