我需要Delphi的文件下载程序组件.你可以帮我吗?
解决方法
使用高级
URLDownloadToFile
功能:
uses UrlMon; ... URLDownloadToFile(nil,'http://www.rejbrand.se/','C:\Users\Andreas Rejbrand\Desktop\index.html',nil);
或者,您可以使用WinInet函数轻松编写自己的下载程序函数,例如
uses WinInet; ... hInet := InternetOpen(PChar(UserAgent),INTERNET_OPEN_TYPE_PRECONFIG,nil,0); try hURL := InternetOpenUrl(hInet,PChar('http://' + Server + Resource),0); try repeat InternetReadFile(hURL,@Buffer,SizeOf(Buffer),BufferLen); ...
更新
我写了一个小样本.您可能希望在自己的线程中执行此代码,并让它每10 kB左右进行一次ping操作,以便您可以为用户提供一些进度条.
function DownloadFile(const UserAgent,URL,FileName: string): boolean; const BUF_SIZE = 4096; var hInet,hURL: HINTERNET; f: file; buf: PByte; amtc: cardinal; amti: integer; begin result := false; hInet := InternetOpen(PChar(UserAgent),0); try hURL := InternetOpenUrl(hInet,PChar(URL),0); try GetMem(buf,BUF_SIZE); try FileMode := fmOpenWrite; AssignFile(f,FileName); try Rewrite(f,1); repeat InternetReadFile(hURL,buf,BUF_SIZE,amtc); BlockWrite(f,buf^,amtc,amti); until amtc = 0; result := true; finally CloseFile(f); end; finally FreeMem(buf); end; finally InternetCloseHandle(hURL); end; finally InternetCloseHandle(hInet); end; end;