下载文件 Get File From the Net

Get File From the Net
Create a Delphi application that downloads files from the Internet; add an auto-update feature to your programs. <!-- BEGIN FREQUENCY TEMPLATE -->
 Join the Discussion
"Post your views, or look for guidance on the questions and issues that matter to creating a simple ftp download manager in Delphi"
Discuss!
  Related Resources
· Copy files with Delphi
· Internet Programming
· My IP with Delphi
· Are we OnLine?
 Elsewhere on the Web
· Console type FTP
· WinInet API reference
<!-- Begin Poll Code -->
About Poll
This [013001] article is:
Awesome
Great
Ok
Not bad
Poor
Current Results
<!-- End Poll Code -->
 
<!--END FREQUENCY TEMPLATE-->

<!-- BEGIN ARTICLE CONTENT -->

If your program relies on Packages or DLLs than deploying new versions of your libraries certainly takes some time. The Internet now provides a fairly easy way to accomplish this task. Adding an auto-update option to your applications could be the best way to keep your Delphi applications up to date.

Let's see how to create the most important piece of code in any FTP application.

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). For example, we could use the functions inside the WinInet API to: add an FTP browser to any application, create an application that automatically downloads files from a public FTP site or search the Internet site for references to graphics and download only the graphics.

GetInetFile function

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;
 

Note: In order to provide some visual feedback to the user you could add a line like FlashWindow(Application.Handle,True) in the body of the repeat/until block. The FlashWindow API call flashes the caption of your applications name in the task bar.

Usage
To call the GetInetFile function you could use the next peace of code:

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;
 

This code will get the 'forminbpl.zip' file from this site, download it, and save it as 'File Downloaded From the Net.zip'.

Note: Depending on which version of Delphi you have, several components can be found on the Internet page of the VCL palette that are designed to ease the task of Internet enabling your applications. For an example of a FTP component look for TNMFTP in the FastNet VCL page. <!-- END ARTICLE CONTENT -->

<!-- START ARTICLE NEW BOTTOM -->

<!-- START ARTICLE NEW BOTTOM -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值