| Blog信息 |
|
blog名称:注册会计师(注会)练习软件 日志总数:398 评论数量:116 留言数量:27 访问次数:3284586 建立时间:2005年6月6日 |

| |
|
[delpih编程]delphi 中用NMFTP实现下载 软件技术
吕向阳 发表于 2009/6/19 21:27:32 |
| 代码如下 unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, Psock, NMHttp, ExtCtrls, jpeg; type TfrmDownload = class(TForm) NMHTTP1: TNMHTTP; btnGet: TButton; Label1: TLabel; edtURL: TEdit; StatusBar1: TStatusBar; Label2: TLabel; tempFileName: TLabel; Label4: TLabel; finalFileName: TLabel; ProgressBar1: TProgressBar; btnExit: TButton; procedure btnGetClick(Sender: TObject); procedure NMHTTP1Success(Cmd: CmdType); procedure FormCreate(Sender: TObject); procedure NMHTTP1Failure(Cmd: CmdType); procedure NMHTTP1PacketRecvd(Sender: TObject); procedure btnExitClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmDownload: TfrmDownload; implementation {$R *.DFM} procedure TfrmDownload.btnGetClick(Sender: TObject); begin NMHTTP1.Get(edtURL.text); // 下载URL指定的文件 end; procedure TfrmDownload.NMHTTP1Success(Cmd: CmdType); var filName:string; buf:string; pnewfile,poldfile:pchar; i:integer; begin // 获取临时文件名 filName:=nmhttp1.body; // 获取文件名 for i:=length(edtURL.Text) downto 1 do if edtURL.text[i]<>'/' then buf:=edtURL.text[i]+buf // 获取URL所指文件名 else break; // 获取下载的临时文件指针 Getmem(poldFile,length(filName)+1); StrPCopy(poldFile,filName); // 为URL所指文件分配内存 Getmem(pnewfile,length(buf)+1); StrPCopy(pnewfile,buf); // 将下载文件名重命名为URL所指文件名 MoveFile(poldfile,pnewfile); // 释放文件指针所指的内存 Freemem(poldfile); Freemem(pnewfile); // 显示相关信息 statusbar1.Panels.Items[0].text:='下载完成'; tempFileName.caption:= filName; finalFileName.Caption:=buf; end; procedure TfrmDownload.FormCreate(Sender: TObject); begin //保存获得的文件 NMHTTP1.InputFileMode:= TRUE; NMHTTP1.TimeOut:=2000; //设置临时文件名 NMHTTP1.Body:='tempFile'; NMHTTP1.Header:='HeaderFile'; // 初始化进度条 ProgressBar1.Min:=0; ProgressBar1.Max:=100; ProgressBar1.Position:=0; end; procedure TfrmDownload.NMHTTP1Failure(Cmd: CmdType); begin StatusBar1.Panels.Items[0].text:='操作失败'; end; procedure TfrmDownload.NMHTTP1PacketRecvd(Sender: TObject); begin // 显示下载进度 ProgressBar1.Position := Round( NMHTTP1.BytesRecvd /NMHTTP1.BytesTotal)*100; end; procedure TfrmDownload.btnExitClick(Sender: TObject); begin frmDownload.Close; end; end. |
|
|