Recent

Author Topic: Download form - which library to use?  (Read 17147 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Download form - which library to use?
« Reply #15 on: July 12, 2013, 05:19:42 pm »
Hmmm, that code looks very familiar ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BeniBela

  • Hero Member
  • *****
  • Posts: 958
    • homepage
Re: Download form - which library to use?
« Reply #16 on: July 12, 2013, 10:08:16 pm »
Everyone is always writing this so needless complicated.

With my Internet Tools, getting the absolute url of the first to an exe on that page:

Code: [Select]
uses simpleinternet;
var
  listingPage, absoluteUrlToFirstExe: String;
begin
  listingPage := 'http://get.videolan.org:81/vlc/last/win32/';
  absoluteUrlToFirstExe := process(listingPage, 'resolve-uri(//a/@href[ends-with(., "exe")], "'+listingPage+'")').toString;
  showMessage(absoluteUrlToFirstExe);
end;

mesiarm

  • New Member
  • *
  • Posts: 45
Re: Download form - which library to use?
« Reply #17 on: July 12, 2013, 10:56:41 pm »
Yes it is much faster and easier but it doesnt support Onstatus - for progressbar to work....

mesiarm

  • New Member
  • *
  • Posts: 45
Re: Download form - which library to use?
« Reply #18 on: July 12, 2013, 11:17:17 pm »
and Jurassic Pork ... I found it, but it is procedure in your case and I don´t see how your code works...because with this code I will ask server for directory index file... it will return it with Resultcode 200 and then change file to read? should´t it be sooner?

BeniBela

  • Hero Member
  • *****
  • Posts: 958
    • homepage
Re: Download form - which library to use?
« Reply #19 on: July 13, 2013, 12:10:09 am »
but it doesnt support Onstatus - for progressbar to work....

Why would you want to show a progressbar while downloading the directory listing?
That should be almost instantaneously.

Anyways:

Code: [Select]
uses simpleinternet;
procedure tform.progress(sender: TObject; progress, maxprogress: longint);
begin
  progressbar1.position := progress;
  progressbar1.max := maxprogress;
end;

procedure tform.download;
var
  listingPage, absoluteUrlToFirstExe: String;
begin
  needInternetAccess;
  defaultInternet.OnProgress:=@progress;
  listingPage := 'http://get.videolan.org:81/vlc/last/win32/';
  absoluteUrlToFirstExe := process(listingPage, 'resolve-uri(//a/@href[ends-with(., "exe")], "'+listingPage+'")').toString;
  showMessage(absoluteUrlToFirstExe);
end;

on Windows at least.
(on Linux it does not work. More precise, on Linux progress is the same value as synapse's THttpSend.DownloadSize, which seems to be not what I expected it to be)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Download form - which library to use?
« Reply #20 on: July 13, 2013, 02:21:09 am »
hello,

BeniBela : read the entire post . mesiarm wants to download a big file but for this he wants to know the last name of the file ( with the version in the name).

Mesiarm : Sorry  :-[  i show you a wrong way. Forget the ListHTTPFiles.
to do what you want :
1 - get the Filename of the last version of vlc
2 - get the Url from which the file will be downloaded.
3 - download the file

to do this a function to extract text from URL ( on 200 (OK) or 302 (redirect) result code )
Code: [Select]
function GetTextFromURL(URL,regExpress: string): String;
var
  HTTPGetResult: Boolean;
  HTTPSender: THTTPSend;
  TextHttp : TStringList;
  x: integer;
  re: TRegExpr;
  s : string;
begin
  Result := '';
  HTTPSender := THTTPSend.Create;
  try
    HTTPGetResult := HTTPSender.HTTPMethod('GET', URL);
    if (HTTPSender.ResultCode = 200) or
       (HTTPSender.ResultCode = 302) then
     begin
       Texthttp := TStringList.Create();
       Texthttp.LoadFromStream(HTTPSender.Document);
       re := TRegExpr.Create;
       // expression to extract text from URL :
       re.Expression := regexpress;
       For x := 0 to Texthttp.Count -1  do
       begin
       s := Texthttp.Strings[x];
       if re.Exec(s) then Result := re.Match[1];
       end;
       re.Free;
       Texthttp.Free;
     end;
  finally
    HTTPSender.Free;
  end;
end;

code to download the file :
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
  var i:integer;
  URLtoDL,FileNameToDL:string;
  download: THTTPDownload;
begin
  FileNameToDL := GetTextFromURL('http://get.videolan.org:81/vlc/last/win32/',
                  '<li><a href=\"(.*\.exe)\".+');
  if FileNameToDL <> '' then
  begin
  URLtoDL := GetTextFromURL('http://get.videolan.org:81/vlc/last/win32/' +
             FileNameToDL,'<p>The document has moved <a href=\"(.*\.exe)\".+');
  if URLtoDL <> '' then
    begin
     Updater.Progressbar1.Position := 0;
     download:= THTTPDownload.Create(URLtoDL, FileNameToDL);
     download.Resume;
     Updater.ShowModal;
     download.free;
    end;
  end;
end;

if you want more simple, use the great benibela's internet tools   (GPL)
here : http://benibela.de/sources_en.html#internettools

Friendly, J.P
« Last Edit: July 13, 2013, 03:38:42 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

mesiarm

  • New Member
  • *
  • Posts: 45
Re: Download form - which library to use?
« Reply #21 on: July 14, 2013, 11:37:27 am »
Jurrasic Pork thank you very much for your patience with me and for your advices  :) and  BeniBela also thank you  :) And please look at this image - it ´s preview of my project -  http://img199.imageshack.us/img199/5716/ddyb.png Acessing to form .... in main form updater is global variable ... and I have also changed Owner from nil to Application and Showmodal to Show and deleted updater.Free line  but it didnt worked. Now I have added line :
Code: [Select]
if assigned (self) then showmessage ('yes in form activate') else showmessage ('no in form activate');    to Tupdater.Formactivate and it returns yes... so in formactivate and other form methods it works because I have changed updater to self.... But in thread I have to call it as updater because thread is other class and there it returns no (if I call
Code: [Select]
if assigned (updater) then showmessage ('yes in thread') else showmessage ('no in thread'); and even
Code: [Select]
  if fupdater = nil then showmessage ('yes nil') else showmessage ('no nil');  returns yes nil in thread) Please help me ...

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Download form - which library to use?
« Reply #22 on: July 14, 2013, 05:11:48 pm »
hello mesiarm,
the best solution would be to put your whole project in attachment (if it is possible) to see what is wrong.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

mesiarm

  • New Member
  • *
  • Posts: 45
Re: Download form - which library to use?
« Reply #23 on: July 28, 2013, 02:56:06 pm »
Sorry I wasn´t online for long time but now I have created demo project of my problem because I wanted to know where s the problem - here it is : http://uploadir.com/uploads/l79yb6ji/downloads/new

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Download form - which library to use?
« Reply #24 on: July 28, 2013, 05:46:22 pm »
Hello Mesiarm,
i don't know why you don't work with Form2 all the time and why you use fupdater variable.

With those changes it seems that your project run :

1 - remove unit2 from the uses at the beginning of the unit1.
Code: [Select]
interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

2 - replace your code by this code after  implementation :
Code: [Select]
implementation
uses Unit2;  //  here is the trick
{$R *.lfm}

{ TForm1 }

procedure TForm1.btnClick(Sender: TObject);

begin
  Form2.showmodal;
  end;

end.

3 - in the unit2 replace all the occurences of Fupdater by Form2.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

mesiarm

  • New Member
  • *
  • Posts: 45
Re: Download form - which library to use?
« Reply #25 on: July 30, 2013, 08:48:15 pm »
Yes than you... now it works.... and please can you explain me what´s difference in using in interface and implementation part? Idon´t understand that....

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Download form - which library to use?
« Reply #26 on: July 31, 2013, 12:54:01 am »
for example read this about circular unit references
« Last Edit: July 31, 2013, 12:57:48 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

mesiarm

  • New Member
  • *
  • Posts: 45
Re: Download form - which library to use?
« Reply #27 on: August 01, 2013, 11:44:22 pm »
ok thanks :)  I have read it. now I want to implement cancel button and I have downloadthread.terminate command in cancelonclick, but it won´t cancel download... I have read that terminate only send instruction to terminate and i didn´t found way to force terminate thread ...so i wanted to add while not terminated loop.... debbugging I have found that during download is still running this command: 
Code: [Select]
HTTPGetResult:=http.HTTPMethod('GET', fURL); so i have added
Code: [Select]
while not terminated do HTTPGetResult:=http.HTTPMethod('GET', fURL); It didn´t worked... I wanted to use something like Application.ProcessMessages and since I can´t use this command inside thread I tried this code which i had found
Code: [Select]
  if MsgWaitForMultipleObjects(0, nil, FALSE, 10, QS_ALLINPUT) = WAIT_OBJECT_0 then
    begin
      while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
      begin
        TranslateMessage(Msg);
        DispatchMessage(Msg);
      end;
    end;   
without success. What can I do with it?

mesiarm

  • New Member
  • *
  • Posts: 45
Re: Download form - which library to use?
« Reply #28 on: September 19, 2013, 10:45:16 pm »
Hm I found it, with http.Abort.... but Jurrasic Pork you have earlier gave me function GetTextFromURL and it worked till now...please look at it...  now I don´t know, why it stopped working and although it with redirect with code you have said to me, when it connect to server, it will return 302 Moved Temporarily

 

TinyPortal © 2005-2018