2006/04/21 | 自动下载文件
类别(语言类学习笔记) | 评论(0) | 阅读(307) | 发表于 14:07
//用了nmhttp这个控件,还有类似这种的网上应该很多例子的,delphifans里好像就不少 <BR /> <BR />
<BR /> <BR />
unit Unit1; <BR /> <BR />
<BR /> <BR />
interface <BR /> <BR />
<BR /> <BR />
uses <BR /> <BR />
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, <BR /> <BR />
Dialogs, StdCtrls, Psock, NMHttp, ComCtrls, ExtCtrls, <BR /> <BR />
Buttons,inifiles, shellapi; <BR /> <BR />
<BR /> <BR />
type <BR /> <BR />
TForm1 = class(TForm) <BR /> <BR />
NMHTTP1: TNMHTTP; <BR /> <BR />
Timer1: TTimer; <BR /> <BR />
Panel1: TPanel; <BR /> <BR />
Label3: TLabel; <BR /> <BR />
Animate1: TAnimate; <BR /> <BR />
ProgressBar1: TProgressBar; <BR /> <BR />
Label1: TLabel; <BR /> <BR />
Label2: TLabel; <BR /> <BR />
Timer2: TTimer; <BR /> <BR />
Image1: TImage; <BR /> <BR />
procedure Timer1Timer(Sender: TObject); <BR /> <BR />
procedure FormShow(Sender: TObject); <BR /> <BR />
procedure Timer2Timer(Sender: TObject); <BR /> <BR />
private <BR /> <BR />
{ Private declarations } <BR /> <BR />
public <BR /> <BR />
{ Public declarations } <BR /> <BR />
url,cflj,bbh,bz:string; <BR /> <BR />
i,size:integer; <BR /> <BR />
end; <BR /> <BR />
<BR /> <BR />
var <BR /> <BR />
Form1: TForm1; <BR /> <BR />
<BR /> <BR />
implementation <BR /> <BR />
<BR /> <BR />
{$ R *.dfm} <BR /> <BR />
<BR /> <BR />
procedure TForm1.Timer1Timer(Sender: TObject); <BR /> <BR />
begin <BR /> <BR />
ProgressBar1.Position:=nmhttp1.BytesRecvd; <BR /> <BR />
label2.Caption:='已下载:'+inttostr(nmhttp1.BytesRecvd)+' 字节';; <BR /> <BR />
end; <BR /> <BR />
<BR /> <BR />
procedure TForm1.FormShow(Sender: TObject); <BR /> <BR />
var <BR /> <BR />
inif:Tinifile; <BR /> <BR />
begin <BR /> <BR />
try <BR /> <BR />
cflj:=extractfilepath(application.ExeName)+'ptckhd.exe'; <BR /> <BR />
inif:=Tinifile.Create(extractfilepath(application.ExeName)+'config.ini'); <BR /> <BR />
url:=inif.ReadString('zdgx','url',''); <BR /> <BR />
bbh:=inif.ReadString('zdgx','bbh',''); <BR /> <BR />
finally <BR /> <BR />
inif.free; <BR /> <BR />
end; <BR /> <BR />
nmhttp1.Head(url); <BR /> <BR />
label1.Caption:='共:'+inttostr(nmhttp1.BytesTotal)+' 字节'; <BR /> <BR />
label2.Caption:='已下载:0 字节'; <BR /> <BR />
ProgressBar1.Min:=0; <BR /> <BR />
ProgressBar1.Max:=nmhttp1.BytesTotal; <BR /> <BR />
size:=nmhttp1.BytesTotal; <BR /> <BR />
nmhttp1.Cancel; <BR /> <BR />
end; <BR /> <BR />
<BR /> <BR />
procedure TForm1.Timer2Timer(Sender: TObject); <BR /> <BR />
var <BR /> <BR />
inif:Tinifile; <BR /> <BR />
begin <BR /> <BR />
i:=i+1; <BR /> <BR />
if i=3 then <BR /> <BR />
begin <BR /> <BR />
timer1.Enabled:=true; <BR /> <BR />
Animate1.Active:=true; <BR /> <BR />
nmhttp1.InputFileMode:=true; <BR /> <BR />
nmhttp1.Body:=cflj; <BR /> <BR />
nmhttp1.Get(url); <BR /> <BR />
try <BR /> <BR />
inif:=Tinifile.Create(extractfilepath(application.ExeName)+'config.ini'); <BR /> <BR />
inif.WriteString('bbh','bbh',bbh); <BR /> <BR />
shellexecute(handle,nil,pchar('ptckhd.exe'),nil,nil,sw_shownormal); <BR /> <BR />
finally <BR /> <BR />
inif.free; <BR /> <BR />
close; <BR /> <BR />
end; <BR /> <BR />
end; <BR /> <BR />
end; <BR /> <BR />
<BR /> <BR />
end. <BR /> <BR />
<BR />
<BR />
<BR />
<BR />
<BR />
//用了nmhttp这个控件,还有类似这种的网上应该很多例子的,delphifans里好像就不少 <BR />
<BR />
unit uMain; <BR />
<BR />
interface <BR />
<BR />
uses <BR />
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, <BR />
Dialogs, StdCtrls, Psock, NMHttp, ComCtrls, ExtCtrls, <BR />
Buttons,inifiles, shellapi; <BR />
<BR />
type <BR />
TForm1 = class(TForm) <BR />
<BR />
Timer1: TTimer; <BR />
Timer2: TTimer; <BR />
Label1: TLabel; <BR />
Label2: TLabel; <BR />
Label3: TLabel; <BR />
Panel1: TPanel; <BR />
ProgressBar1: TProgressBar; <BR />
Image1: TImage; <BR />
NMHTTP1: TNMHTTP; <BR />
btn1: TButton; <BR />
procedure Timer1Timer(Sender: TObject); <BR />
procedure FormShow(Sender: TObject); <BR />
procedure Timer2Timer(Sender: TObject); <BR />
procedure btn1Click(Sender: TObject); <BR />
private <BR />
{ Private declarations } <BR />
public <BR />
{ Public declarations } <BR />
url,bbh,bz:string; <BR />
i,size:integer; <BR />
end; <BR />
<BR />
var <BR />
Form1: TForm1; <BR />
<BR />
implementation <BR />
<BR />
{$ R *.dfm} <BR />
<BR />
procedure TForm1.Timer1Timer(Sender: TObject); <BR />
begin <BR />
ProgressBar1.Position:=nmhttp1.BytesRecvd; <BR />
label2.Caption:='已下载:'+inttostr(nmhttp1.BytesRecvd)+' 字节';; <BR />
end; <BR />
<BR />
procedure TForm1.FormShow(Sender: TObject); <BR />
var <BR />
inif:Tinifile; <BR />
begin <BR />
inif:=Tinifile.Create(extractfilepath(application.ExeName)+'config.ini'); <BR />
try <BR />
url:=inif.ReadString('URL','lURL',''); <BR />
bbh:=inif.ReadString('File','lFIle',''); <BR />
finally <BR />
inif.free; <BR />
end; <BR />
try <BR />
nmhttp1.Head(url); <BR />
label1.Caption:='共:'+inttostr(nmhttp1.BytesTotal)+' 字节'; <BR />
label2.Caption:='已下载:0 字节'; <BR />
ProgressBar1.Min:=0; <BR />
ProgressBar1.Max:=nmhttp1.BytesTotal; <BR />
size:=nmhttp1.BytesTotal; <BR />
nmhttp1.Cancel; <BR />
except <BR />
on E: exception do showmessage(e.Message) <BR />
end; <BR />
end; <BR />
<BR />
procedure TForm1.Timer2Timer(Sender: TObject); <BR />
var <BR />
inif:Tinifile; <BR />
begin <BR />
i:=i+1; <BR />
if i=3 then <BR />
begin <BR />
timer1.Enabled:=true; <BR />
nmhttp1.InputFileMode:=true; <BR />
nmhttp1.Body:=bbh; <BR />
nmhttp1.Get(url); <BR />
try <BR />
inif:=Tinifile.Create(extractfilepath(application.ExeName)+'config.ini'); <BR />
inif.WriteString('bbh','lbbh',bbh); <BR />
shellexecute(handle,nil,pchar(bbh),nil,nil,sw_shownormal); <BR />
finally <BR />
inif.free; <BR />
close; <BR />
end; <BR />
end; <BR />
end; <BR />
<BR />
procedure TForm1.btn1Click(Sender: TObject); <BR />
var <BR />
inif:Tinifile; <BR />
begin <BR />
with Tinifile.Create(extractfilepath(application.ExeName)+'config.ini') do <BR />
try <BR />
WriteString('URL','LURL','http://www.zzyingrun.com/licai/licai.rar'); <BR />
WriteString('File','lFile',ExtractfilePath(application.exename)+'licai.rar'); <BR />
finally <BR />
free; <BR />
end; <BR />
end; <BR />
<BR />
end. <BR />
<BR />







----------------------------------





unit UpDownFile;

interface

uses
Windows, Classes, Idhttp, URLMon, IdMultipartFormData;

const UpUrl = 'http://127.0.0.1/upfile/upfile.asp';
const UpFileName = 'C:\Documents and Settings\Administrator\桌面\test\web.mdb';
const DownUrl = 'http://www.google.com/images/logo_sm.gif';
const DownFileName = 'web.gif';

type
TUpDownFile = class
private
FThreadID : DWord;
FHandle : THandle;
{ Private declarations }
public
constructor Create;
procedure UpFile;
procedure DownFile;
procedure Close;
end;

implementation

function UpFileEx(): string; stdcall;
var
MutPartForm: TIdMultiPartFormDataStream;
response: string;
FHttp: Tidhttp;
begin
FHttp := Tidhttp.Create(nil);
FHttp.HandleRedirects := true;
FHttp.AllowCookies := true;

MutPartForm := TIdMultiPartFormDataStream.Create;
MutPartForm.AddFormField('act', 'upload');
MutPartForm.AddFormField('upcount', '1');
MutPartForm.AddFormField('filepath', 'data');
MutPartForm.AddFormField('file1', 'filename="' + UpFileName + '"');
MutPartForm.AddFormField('Submit', 'Submit');
MutPartForm.AddFile('file1', UpFileName, 'text/plain');
try
response := FHttp.Post(UpUrl, MutPartForm);
//Messagebox(0, PAnsiChar(response), 'ca', MB_OK);
finally
MutPartForm.Free;
FHttp.Free;
end;
end;

function DownFileEx(): string; stdcall;
begin
UrlDownloadToFile(nil, PChar(DownUrl), PChar(DownFileName), 0, nil);
end;

constructor TUpDownFile.Create;
begin
//
end;

procedure TUpDownFile.UpFile;
begin
//FHandle := CreateThread(nil,0,@UpFileEx,nil,0,FThreadID);
UpFileEx;
end;

procedure TUpDownFile.DownFile;
begin
// FHandle := CreateThread(nil,0,@DownFileEx,nil,0,FThreadID);
DownFileEx;
end;

procedure TUpDownFile.Close;
begin
//ExitThread(FThreadID);
end;

end.
0

评论Comments