?
Solved

Download multiple files using URLDownloadToFile

Posted on 2003-03-29
9
Medium Priority
?
834 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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