?
Solved

Download multiple files using URLDownloadToFile

Posted on 2003-03-29
9
Medium Priority
?
861 Views
Last Modified: 2012-08-13
Hi,
I want to use a loop to download a series of files. Files are numbered
consecutively, e.g.
10.jpg
11.jpg
12.jpg
If I run the code below only the first file is downloaded.

******************************
procedure TForm1.Button1Click(Sender: TObject);
var
  A, B: array[0..79] of Char;
  Filename: String;
  Error, I: Word;
  T1: Integer;
begin
  for I := 10 to 12 do begin
    Filename := Format('%d.jpg', [I]);
    StrPCopy(A, 'http://users.pandora.be/nenya/test/' + Filename);
    StrPCopy(B, 'C:\' + Filename);
    Error := URLDownloadToFile(nil, A, B, 0, nil);
    //*********
    //********* wait for the download to be complete
    //********* (How do I know that? Is waiting sufficient,
    //********* or even necessary?)
    //*********
  end;
end;
******************************

I tried to add a (long) delay at the end of the loop (calling
ProcessMessages), but this doesn't work.
Would a BindStatusCallback help? How do I implement it?
Any ideas? TIA

Steven
0
Comment
Question by:stevenvh
8 Comments
 
LVL 3

Expert Comment

by:emadat
ID: 8231020
Hi Steven

Here is a you function after being modfied to do the job.

I tested it and it works downloading three red, green and blue rectangles.

You do not need a delay after the URLDownloadToFile function as it is a blocking call. i.e. the call will not return unless it finishes its download


//************************************************
procedure TForm1.Button1Click(Sender: TObject);
var
     sURL,
     SLocalFileName,
     sFilename          : String;
     Error, I          : Word;
begin
     for I := 10 to 12 do begin
          sFilename := IntToStr(i) + '.jpg';
          sURL := 'http://users.pandora.be/nenya/test/' + sFilename;
          sLocalFileName := 'c:\' + sFilename;
          Error := URLDownloadToFile(nil, PChar(sURL), PChar(sLocalFileName), 0, nil);
     end;
end;
//************************************************

Regards,
Emad
0
 

Expert Comment

by:-TM-
ID: 8231082
if UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, 0) = 0 then
MessageBox(0,'Download Succesful','Download',MB_ICONINFORMATION)
else
MessageBox(0,'Download Error','Error',MB_ICONSTOP);
0
 

Author Comment

by:stevenvh
ID: 8233186
Emad,
your code isn't basically different from mine: it uses PChar instead of StrPCopy. Both create null-terminated strings.
Result: it doesn't work any better than my procedure on my PC. It downloads the first file, but doesn't do the other ones.
Could my firewall (ZoneAlarm) have something to with it?

TM,
your code only reports the error, but doesn't solve the problem, i.e. only one file is downloaded.

Thanks for the replies to both of you anyway.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:stevenvh
ID: 8233618
Emad,
your code isn't basically different from mine: it uses PChar instead of StrPCopy. Both create null-terminated strings.
Result: it doesn't work any better than my procedure on my PC. It downloads the first file, but doesn't do the other ones.
Could my firewall (ZoneAlarm) have something to with it?

TM,
your code only reports the error, but doesn't solve the problem, i.e. only one file is downloaded.

Thanks for the replies to both of you anyway.

0
 

Expert Comment

by:Churo
ID: 8240415
ummm, i think u must use TThread to do this work. Create as suspended and pass the jpg as a property then resume it.
0
 
LVL 3

Expert Comment

by:emadat
ID: 8240600
Steven,
I can not see your problem so far.!
I tried the code and it works fine.

If you are running any type of personal Firewall; try disabling it and test your code -or mine- and it should work.

Back to your comments:

   //*********
   //********* wait for the download to be complete
   //********* (How do I know that? Is waiting sufficient,
   //********* or even necessary?)
   //*********

There is no need to wait. The function "URLDownloadToFile" will return only if it either fails or downloads the file.!
So you should check the result before continuing in the loop

Emad
0
 

Accepted Solution

by:
stevenvh earned 0 total points
ID: 9062697
Moved to another PC, with a router/switch connection to the 'Net. The switch has a built-in firewall, so I didn't install Zonealarm.
And lo and behold: now my original code works. So I guess it was caused by the Zonealarm after all.
0
 

Expert Comment

by:YensidMod
ID: 9063006
To All Experts,

A request has been made in Community Support to close this question:

http://www.experts-exchange.com/Community_Support/CleanUp/Q_20698168.html

If there are no objections, after 72 hrs, a moderator will finalize this question by:

- Saving this Q as a PAQ and refunding the points

Please leave any recommendations here.

Best regards,

YensidMod
Community Support Moderator @Experts Exchange
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

615 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