Re: How to download a file?


Posted by webmaster Guido on October 20, 2000 at 16:34:29:

In Reply to: How to download a file? posted by emarti Murat Goldman on October 18, 2000 at 02:06:25:

: Hi,
: How to download a file with Delphi 4 C/S ?
: For example: http://www.xxx.com/image.jpg
: or
: http://www.mypage.com/write.txt
------
Surprisingly, this can be done very easily without a component, only by using the Windows API function URLDownloadToFile(). It's a pity that this function is not documented in Delphi's help, nor in the WinAPI help.

To begin with, add "URLMon" to the unit's USES clause:
uses Windows, Messages,... URLMon;

Below is a very basic example of the use of URLDownloadToFile. In an application, you would have to give some feedback to the user of what's happening, such as: disable the button until the end of the download, show some message in a status bar, show the "hourglass" cursor.

procedure TForm1.Button1Click(Sender: TObject);
var
SourceFile, LocalFile: string;
begin
SourceFile := 'http://www.xyz.com/image.jpg';
LocalFile := 'c:\tests\test.jpg';
if URLDownloadToFile(nil, PChar(SourceFile), PChar(LocalFile), 0, nil) = 0 then
DoSomethingWithTheFile
else
ShowMessage('Error downloading ' + SourceFile);
end;



Related Articles and Replies: