首页  编辑  

读取、下载一个网络文件

Tags: /超级猛料/Network.网络通讯/其它/   Date Created:

访问下载互联网上面的文件,在猛料以前的资料里面也有,利用的是internet API UrlDownloadToFile来完成的。下面的函数是利用Internet API的读取文件函数来做的:

http://delphi.about.com/library/weekly/aa013001a.htm

uses Wininet;

function GetInetFile

( const fileURL, FileName: String ): boolean;

const BufferSize = 1024;

var

 hSession, hURL: HInternet;

 Buffer: array [1..BufferSize] of Byte;

 BufferLen: DWORD;

 f: File ;

 sAppName: string ;

begin

Result:=False;

sAppName := ExtractFileName(Application.ExeName);

hSession := InternetOpen(PChar(sAppName),

               INTERNET_OPEN_TYPE_PRECONFIG,

              nil , nil , 0);

try

 hURL := InternetOpenURL(hSession,

           PChar(fileURL),

            nil ,0,0,0);

  try

  AssignFile(f, FileName);

  Rewrite(f,1);

  repeat

   InternetReadFile(hURL, @Buffer,

                    SizeOf(Buffer), BufferLen);

   BlockWrite(f, Buffer, BufferLen)

  until BufferLen = 0;

  CloseFile(f);

  Result:=True;

  finally

  InternetCloseHandle(hURL)

  end

finally

 InternetCloseHandle(hSession)

end

end ;

var FileOnNet, LocalFileName: string

begin

FileOnNet:=

 'http://delphi.about.com/library/forminbpl.zip';

LocalFileName:='File Downloaded From the Net.zip'

if GetInetFile(FileOnNet,LocalFileName)=True then

 ShowMessage('Download successful')

else

 ShowMessage('Error in file download')

end ;