Solved

Downloading multiple images from URL FAST

Posted on 2007-11-27
15
252 Views
Last Modified: 2010-04-05
Hi expert,

I need to download a list of images from a URL's FAST!

I guess the faster's way is to use Threads?

If so, could someone please give me an example code?

Or if anyone knows a faster way...

Hope you can help

thanks

ST3VO
0
Comment
Question by:ST3VO
  • 9
  • 6
15 Comments
 
LVL 10

Expert Comment

by:dinilud
ID: 20357349
0
 
LVL 10

Expert Comment

by:dinilud
ID: 20357358
See rllibby's solution from the above link
0
 

Author Comment

by:ST3VO
ID: 20357426
Hmmm....I can compile it without errors but when I try to run the application nothing happens :o/

Any ideas why???
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:ST3VO
ID: 20357569
OK...got it running....

But how do I get this code to actually download files from the urls???

It's downloading nothing although I've changed the links the url's that link to images!

0
 

Author Comment

by:ST3VO
ID: 20357594
Url's I've inserted in the listbox format are:

for example:
http://www.somesite.com/animage.gif

0
 
LVL 10

Accepted Solution

by:
dinilud earned 500 total points
ID: 20358109
please try this

Unit1.dfm
===========
 
object Form1: TForm1
  Left = 10
  Top = 101
  Width = 779
  Height = 479
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 212
    Top = 44
    Width = 545
    Height = 385
    Columns = <
      item
        Caption = 'URL'
        Width = 120
      end
      item
        Caption = 'Result'
        Width = 200
      end>
    TabOrder = 0
    ViewStyle = vsReport
  end
  object Button1: TButton
    Left = 8
    Top = 12
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object ListBox1: TListBox
    Left = 8
    Top = 44
    Width = 197
    Height = 385
    ItemHeight = 13
    Items.Strings = (
      'http://coastalkarate.com/files/image/gallery/00.jpg'
      'http://coastalkarate.com/files/image/gallery/01.jpg'
      'http://coastalkarate.com/files/image/gallery/02.jpg'
      'http://coastalkarate.com/files/image/gallery/03.jpg'
      'http://coastalkarate.com/files/image/gallery/04.jpg'
      'http://coastalkarate.com/files/image/gallery/05.jpg'
      'http://coastalkarate.com/files/image/gallery/06.jpg'
      'http://coastalkarate.com/files/image/gallery/07.jpg'
      'http://coastalkarate.com/files/image/gallery/08.jpg'
      'http://coastalkarate.com/files/image/gallery/09.jpg'
      'http://coastalkarate.com/files/image/gallery/10.jpg'
      'http://coastalkarate.com/files/image/gallery/11.jpg'
      'http://coastalkarate.com/files/image/gallery/12.jpg'
      'http://coastalkarate.com/files/image/gallery/13.jpg'
      'http://coastalkarate.com/files/image/gallery/14.jpg'
      'http://coastalkarate.com/files/image/gallery/15.jpg'
      'http://coastalkarate.com/files/image/gallery/16.jpg')
    TabOrder = 2
  end
  object UrlDownload1: TUrlDownload
    OnUrlOpen = UrlDownload1UrlOpen
    OnUrlError = UrlDownload1UrlError
    OnUrlComplete = UrlDownload1UrlComplete
    UserAgent = 'TUrlDownload'
    Left = 156
    Top = 8
  end
end
 
 
Unit1.pas
===========
 
unit Unit1;
 
interface
 
// UrlDown can be obtained from my site @
//
//   http://users.adelphia.net/~rllibby/downloads/urldown.zip
//
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, UrlDown;
 
type
  TForm1            =  class(TForm)
     UrlDownload1:  TUrlDownload;
     ListView1:     TListView;
     Button1:       TButton;
     ListBox1:      TListBox;
     procedure      Button1Click(Sender: TObject);
     procedure      UrlDownload1UrlOpen(Sender: TObject; ID: Cardinal);
     procedure      UrlDownload1UrlError(Sender: TObject; ID, Error: Cardinal);
     procedure      UrlDownload1UrlComplete(Sender: TObject; ID, TotalMilliseconds: Cardinal; Stream: TStream);
    procedure FormCreate(Sender: TObject);
  private
     // Private declarations
     FDir:String;
     function GetFileName(Url:String):String;
  public
     // Public declarations
  end;
 
var
  Form1:            TForm1;
 
implementation
{$R *.DFM}
 
function TForm1.GetFileName(Url:String):String;
var i:Integer;
begin
  Result:=url;
  for i:=length(Url) downto 1 do
  begin
    if Url[i]='/' then
    begin
      Result:=copy(Url,i+1,length(Url));
      Break;
    end;
  end;
 
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var  liItem:        TListItem;
     dwIndex:       Integer;
begin
 
  // Walk all url's
  for dwIndex:=0 to Pred(Listbox1.Items.Count) do
  begin
     // Add list item
     liItem:=ListView1.Items.Add;
     // Set list item caption
     liItem.Caption:=Listbox1.Items[dwIndex];
     // Set subitem
     liItem.SubItems.Add('contacting server...');
     // Queue the request
     URLDownload1.OpenRequestEx(Listbox1.Items[dwIndex], nil, Pointer(liItem.Index));
  end;
 
end;
 
procedure TForm1.UrlDownload1UrlOpen(Sender: TObject; ID: Cardinal);
var  liItem:        TListItem;
begin
 
  // Get index from context pointer
  liItem:=ListView1.Items[Integer(URLDownload1.RequestContext[ID])];
 
  // Set subitems text
  liItem.SubItems[0]:='downloading contents...';
 
end;
 
procedure TForm1.UrlDownload1UrlError(Sender: TObject; ID, Error: Cardinal);
var  liItem:        TListItem;
begin
 
  // Get index from context pointer
  liItem:=ListView1.Items[Integer(URLDownload1.RequestContext[ID])];
 
  // Set subitems text
  liItem.SubItems[0]:=Format('failed to get URL data, error code (%d)', [Error]);
 
end;
 
procedure TForm1.UrlDownload1UrlComplete(Sender: TObject; ID, TotalMilliseconds: Cardinal; Stream: TStream);
var  liItem:        TListItem;
     f:String;
begin
 
  // Get index from context pointer
  liItem:=ListView1.Items[Integer(URLDownload1.RequestContext[ID])];
 
  // Resource protection
  try
     F:=FDir+GetFileName(liItem.Caption);
     TMemoryStream(Stream).SaveToFile(f);
     liItem.SubItems[0]:='Compleated';
  finally
  end;
 
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
   FDir:=IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+'DownLoadedFiles\';
   if not DirectoryExists(FDir) then CreateDir(FDir);
end;
 
end.

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20358381
Hi dinilud,

I installed your component and to downloads worked fine, BUT my problem is that I need to download using as many Threads as possible.

This component will download one at a time and I need Multiple threads so I can download as fast as possible.

0
 
LVL 10

Expert Comment

by:dinilud
ID: 20358499
rllibby's this comonent is a mutithreaded component.
You check the updation of download list.
0
 

Author Comment

by:ST3VO
ID: 20358585
I have checked and it's downloading one at a time. I really need speed in downloading above all!

0
 

Author Comment

by:ST3VO
ID: 20358595
Note: Without downloading any download acelerators ;o)
0
 
LVL 10

Expert Comment

by:dinilud
ID: 20358600
0
 
LVL 10

Expert Comment

by:dinilud
ID: 20358667
Check On progress events of that component.
0
 

Author Comment

by:ST3VO
ID: 20358720
UrlDownload1UrlProgress(Sender: TObject; ID,
  TotalSize: Cardinal; Stream: TStream); ???
0
 

Author Comment

by:ST3VO
ID: 20359913
Hi dinilud,

I am going to go for your answer and it does work great. There's just 1 thing I need to know...

How could I get the application to Terminate once the downloads are completed?

Thanks

ST3VO
0
 

Author Comment

by:ST3VO
ID: 20360939
Never mind. I've done it!!!

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question