Several methods of realizing file downloading with Delphi

Source: Internet
Author: User
Tags zip

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;

=================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.