The author recently developed the system needs to write a download file function. Previously used BCB invoke API written very cumbersome, suddenly remembered that there is an API can be done, so early in the morning to search. This API is Urldownloadtofile. Not only that, some of the Delphi control can also be easily implemented download, such as nmhttp, designated Nmhttp1.inputfilemode: = ture; Specifies that the body is a local file name and that a get can be downloaded. Here is the detailed code, all from CSDN. I put them all here for easy access.
uses UrlMon;
function DownloadFile(Source, Dest: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
except
Result := False;
end;
end;
if DownloadFile('http://www.borland.com/delphi6.zip, 'c:\kylix.zip') then
ShowMessage('Download succesful')
else ShowMessage('Download unsuccesful')
========================
Routines:
Uses URLMon, ShellApi;
function DownloadFile(SourceFile, DestFile: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
except
Result := False;
end;
end;
procedure TForm1.Button1.Click(Sender: TObject);
const
// URL Location
SourceFile := 'gif/home_title.gif';
// Where to save the file
DestFile := 'c:\temp\google-image.gif';
begin
if DownloadFile(SourceFile, DestFile) then
begin
ShowMessage('Download succesful!');
// Show downloaded image in your browser
ShellExecute(Application.Handle,PChar('open'),PChar(DestFile),PChar(''),nil,SW_NORMAL)
end
else
ShowMessage('Error while downloading ' + SourceFile)
end;
=================