Your first task is to create a Delphi function used to download a file from the Internet. One way of achieving this task is to use the WinInet API calls. Delphi gives us full access to the WinInet API (wininet.pas) which we can use to connect to and retrieve files from any Web site that uses either Hypertext Transfer Protocol (HTTP) or File Transfer Protocol (FTP). I've already written an article that describes this technique: Get File From the Net.

Another approach, if you have Delphi 6, is to use the TDownloadURL object. The TDownloadURL object, defined in ExtActns.pas unit, is designed for saving the contents of a specified URL to a file. Here's the code that uses the TDownloadURL to download the "What's New and Hot" page from this site.


uses extactns;  // <- must be added //

function Download_HTM(const sURL, sLocalFileName:string): boolean;
begin
Result:=True;
with TDownLoadURL.Create(nil) do
try
   URL:=sURL;
   Filename:=sLocalFileName;
   try
     ExecuteTarget(nil);
   except
     Result:=False
   end;
finally
   Free;
end;
end;





This function, Download_HTM, downloads a file from the URL specified in the sURL parameter, and saves this file locally under a sLocalFileName name. The function returns True if it succeeds, False otherwise. Of course, this function is to be called from the Button1 OnClick event handler. You can see the code below. Note that, locally, the file is saved as c: emp_adp.newandhot.

procedure TForm1.Button1Click(Sender: TObject);
const
ADPNEWHOTURL='http://delphi.about.com/cs/newandhot/index.htm';
TmpFileName='c: emp_adp.newandhot';
begin
if NOT Download_HTM(ADPNEWHOTURL,TmpFileName) then
begin
   ShowMessage('Error in HTML file download');
   Exit;
end;

{
more code to be added
}

end;






Note: In the process of downloading a file, the TDownloadURL periodically generates an OnDownloadProgress event, so that you can provide users with feedback about the process. I'll leave this for you to implement.


facebook posting twit

  • GDI+ 에서 이미지 반투명 처리하기..
  • 자기자신 IP알아내기(로칼컴퓨터)
  • 프로세스명으로 프로세스 죽이는 함수
  • 빈폴더 찾아내기
  • IE 패치에 따른 object, embed, applet 대처 방안
  • 작업관리자에 프로그램 안뜨게 하기
  • String Find Function (StrFnd)
  • Toolbar에서 Icon 없애기
  • 웹브라우져가 떠서 웹페이지 보이게 하는 소스
  • ActiveX에 다이얼로그 붙이기
    Tagged on:         
  • Leave a Reply