My homepage: http://www.tommstudio.com/
The system I recently developed requires a function to download files. I used to write a cumbersome API call using BCB, but suddenly I remembered that there was an API that could be done, so I came to search early in the morning. This API is UrlDownloadToFile. Not only that, some controls of Delphi can also be easily downloaded, such as NMHTTP, specify NMHTTP1.InputFileMode := ture; Specify Body as the local file name, specify Get to download. The following are detailed codes, all from CSDN. I've sorted them all here for easy access to everyone.
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 successful') else ShowMessage('Download unsuccesful') |
=================================
Routine:
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 := ' http://www.google.com/intl/de/images/home_title.gif'; // Where to save the file DestFile := 'c:/temp/google-image.gif'; Begin if DownloadFile(SourceFile, DestFile) then Begin ShowMessage('Download successful!'); // 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 ; |
=======================
Add the following code:
NMHTTP1.InputFileMode := truth; NMHTTP1.Body := 'local file name'; NMHTTP1.Header := 'Head.txt'; NMHTTP1.OutputFileMode := FALSE ; NMHTTP1.ReportLevel := Status_Basic; NMHTTP1.Proxy := 'IP address of proxy server'; NMHTTP1.ProxyPort := 'Port number of proxy server'; With NMHTTP1.HeaderInfo do Begin Cookie := ''; LocalMailAddress := ''; LocalProgram := ''; Referer := ''; UserID := 'User Name'; PassWord := 'user password'; End ; NMHTTP1.Get(' http://www.abcdefg.com/software/a.zip'); |
Try it, there are examples of TNMHTTP controls in Delphi's directory. NT4+, Win95+, IE3+, you can use the URL Moniker function.
uses URLMon; ... OleCheck(URLDownloadToFile( nil ,'URL','Filename',0, nil )); |
You can also pass in an IBindStatusCallback implementation to track the download progress or control the aborted download. A simple occasion can be done in one sentence.
BTW, URL Moniker encapsulates most URLs, instead of encapsulating protocols like NMHTTP, so you can use URLDownloadToFile to download HTTP, FTP and even local and LAN files, as well as other custom monikers, such as MSITSTORE (MSDN Library document moniker implementation ).
var DownloadFile:TFileStream; beginio DownloadFile:=TFileStream.Create('c:/aa.rar',fmCreate); IdHTTP1.Get('http://www.sina.com.cn/download/aa.rar',DownLoadFile); DownloadLoadFile.Free; end ; //----------------------------------------------------------------------------------------------------------------------------- |
Program ends