Link to home
Start Free TrialLog in
Avatar of Grant Fullen
Grant Fullen

asked on

Downloading a large file with delphi "2 Gigs'

I am trying to download a large file using delphi. code below.
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtActns, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
        procedure URLOnDownloadProgress
        (Sender: TDownLoadURL;
         Progress, ProgressMax: Cardinal;
         StatusCode: TURLDownloadStatus;
         StatusText: String; var Cancel: Boolean) ;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.URLOnDownloadProgress;
begin
   ProgressBar1.Max:= ProgressMax;
   ProgressBar1.Position:= Progress;
end;

procedure DoDownload;
begin
   with TDownloadURL.Create(nil) do
   try
     URL:='http://www.grantfullen.com/Delta Force Black Hawk Down.zip';
     FileName := 'c:\dforce.zip';
     OnDownloadProgress := Form1.URLOnDownloadProgress;

     ExecuteTarget(nil) ;
   finally
     Free;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoDownload;
end;

end.
The form hangs and i can not termanate the process. I need to pause and resume the file.
Avatar of aikimark
aikimark
Flag of United States of America image

Do you need to substitute %20 for the space characters in the URL?

Under what circumstances do you need to stop the download process? (user action or download conditions)

Your procedure definition:
procedure URLOnDownloadProgress
        (Sender: TDownLoadURL;
         Progress, ProgressMax: Cardinal;
         StatusCode: TURLDownloadStatus;
         StatusText: String; var Cancel: Boolean)

includes a Cancel parameter.  It would seem reasonable to call this routine with this parameter set to True.
Avatar of Grant Fullen
Grant Fullen

ASKER

Hello thanks for reply.. Yes there is a cancel var but problem is once i press button 1 it locks the form up i have to ctrl,alt,delete to kill it. what am i doing wrong that it is locking the form and can not press button 2.
Thanks
Grant
part of the problem is where your user might click the cancel/stop button and where the download is taking place.  You should open a separate form or async thread to do the download (can be invisible).  You will need to occasionally give up control to Windows in order to process messages (keyboard and mouse actions).
Ok I am trying a thread solution. But same problem form freez up.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ProgressBar1: TProgressBar;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

var
  NewThread: TTestThread;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  NewThread:=TTestThread.Create(False);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  NewThread.Resume;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  NewThread.Suspend;
end;

end.

unit Unit2;

interface

uses
  Classes, Unit1, Windows, Messages, SysUtils, ExtActns;

type
  TTestThread = class(TThread)
  private
    j: Integer;
    { Private declarations }
  protected
    procedure GetInfo;
    procedure Execute; override;
    procedure DoDownload;
    procedure URLOnDownloadProgress
        (Sender: TDownLoadURL;
         Progress, ProgressMax: Cardinal;
         StatusCode: TURLDownloadStatus;
         StatusText: String; var Cancel: Boolean) ;
  end;

implementation

procedure TTestthread.URLOnDownloadProgress;
begin
    Form1.ProgressBar1.Max:= ProgressMax;
   Form1.ProgressBar1.Position:= Progress;
end;

procedure ttestthread.DoDownload;
begin
   with TDownloadURL.Create(nil) do
   try
     URL:='http://mysite.com/largfile.zip';
     FileName := 'c:\LargeFile.zip';
     OnDownloadProgress := URLOnDownloadProgress;

     ExecuteTarget(nil) ;
   finally
     Free;
   end;
end;
procedure TTestThread.GetInfo;
begin
  Form1.Caption:=IntToStr(j);
  DoDownload;
end;

procedure TTestThread.Execute;
var
  i: Integer;
begin
  j:=1;
  FreeOnTerminate:=True;
  for i:=1 to 30000 do
  begin
    if Terminated then Break;
    Synchronize(GetInfo);
    Inc(j, Round(Abs(Sin(Sqrt(i)))));
  end;
end;

end.
If i need to use threads please show me what i am doing wrong.
Thanks
Grant
Add ExtActns to your Uses clause.

Please see this example:
http://delphi.about.com/cs/adptips2003/a/bltip0903_2.htm
Yes that works on a small file it is like i have now. Problem is when you try to do a 2 gig file the form lockes up and I can not press button 2 to cancel .
Thanks
Grant
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial