//-------------------容易的-----------------------------------------用indy控件: IdHTTP1: TIdHTTP; ... procedure TForm1.Button1Click(Sender: TObject); var tmpStream:TFileStream; begin tmpStream:=TFileStream.Create('c:\some.exe',fmCreate); try idhttp1.Get(http://192.168.0.1/some.exe',tmpStream); finally tmpStream.Free; end; end;//-----------------------------/较为复杂的------------------------ bakdir:='c:\bak'; mynet:=GetNetDomain; querycommon.Close; querycommon.SQL.Clear; querycommon.SQL.Add('select * from jtsjk.ftpxxb where domain='''+mynet+''''); querycommon.Open; if querycommon.RecordCount>0 then myftp:=querycommon.fieldbyname('ftphost').asstring else MessageDlg('该网段的FTP主机地址丢失,请通知系统管理员!',mtWarning, [mbOk], 0); with querycommon do begin close; sql.clear; sql.Add('select * from jtsjk.BBXXB where mkmc='''+mkmc+''' and beiy1='''+publicform.ywqdm+''''); open; if recordcount>0 then begin if fieldbyname('zxbb').asstring>bbh then begin if (fieldbyname('gxbz').asstring<>'1')and(fieldbyname('gxbz').asstring<>'0') then begin close;exit;end; if fieldbyname('gxbz').asstring='1' then if infoselform.showbox('版本已经更新,是否现在下载新软件?')=false then begin close;exit;end; publicform.Tag:=9; if fieldbyname('gxbz').asstring='0' then infodlgform.showbox('版本已经更新,现在下载新软件!'); if DirectoryExists(bakdir)=false then MkDir(bakdir); CopyFile(PChar(initdir+'\'+fieldbyname('ftpfile').asstring),PChar(bakdir+'\'+fieldbyname('ftpfile').asstring),false); DeleteFile(initdir+'\'+fieldbyname('ftpfile').asstring); NMFTP1.Host:=myftp; NMFTP1.UserID:=fieldbyname('ftpuser').asstring; NMFTP1.Password:=fieldbyname('ftppass').asstring; try NMFTP1.Connect; except infodlgform.showbox('版本更新时,连接服务器失败!请与市局联系!'); close; exit; end; try s:=fieldbyname('ftpfile').asstring; if fileexists(initdir+'\'+s)=true then begin if DirectoryExists(bakdir)=false then MkDir(bakdir); CopyFile(PChar(initdir+'\'+s),PChar(bakdir+'\'+s),false); if deletefile(initdir+'\'+s)=false then begin infodlgform.showbox('请退出所有程序,并手工更新软件!'); close; exit; end; end; if DirectoryExists(initdir)=false then MkDir(initdir); NMFTP1.ChangeDir(fieldbyname('ftpdir').asstring); NMFTP1.Mode(mode_image); NMFTP1.Download(s,initdir+'\'+s); NMFTP1.Disconnect; except infodlgform.showbox('版本更新时,取文件失败!请与省局联系!'); close; exit; end; infodlgform.showbox('新软件已下载!,请退出并运行文件'+initdir+'\'+s); close; close; sql.clear; sql.add('update ywsjk.YW_DLJLB set dlbz=:dlbz, out_sj= :out_sj where dlgh=:dlgh and dlbz=:dlbz2'); parambyname('dlgh').asstring:=publicform.czygh; parambyname('out_sj').asdatetime:=now; parambyname('dlbz').asstring:='2'; parambyname('dlbz2').asstring:='1'; execsql; winexec(pchar(initdir+'\'+s),sw_normal); application.terminate; end; close; exit; end; close; end; end;
很简单的,一个函数就可以。 uses UrlMonfunction DownloadFile(Source, Dest: string): Boolean; begin try Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0; except Result := False; end; end;procedure TForm1.Button1Click(Sender: TObject); begin if DownloadFile(edit1.Text, edit2.Text) then ShowMessage('下载成功') else ShowMessage('下载失败'); end;
很简单的,一个函数就可以。 uses UrlMonfunction DownloadFile(Source, Dest: string): Boolean; begin try Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0; except Result := False; end; end;procedure TForm1.Button1Click(Sender: TObject); begin if DownloadFile(edit1.Text, edit2.Text) then ShowMessage('下载成功') else ShowMessage('下载失败'); end; ----------------------------------------------------------- 但是只能下载一个文件~下载多个文件就不能编译了~编译时报错
IdHTTP1: TIdHTTP;
...
procedure TForm1.Button1Click(Sender: TObject);
var
tmpStream:TFileStream;
begin
tmpStream:=TFileStream.Create('c:\some.exe',fmCreate);
try
idhttp1.Get(http://192.168.0.1/some.exe',tmpStream);
finally
tmpStream.Free;
end;
end;//-----------------------------/较为复杂的------------------------
bakdir:='c:\bak';
mynet:=GetNetDomain;
querycommon.Close;
querycommon.SQL.Clear;
querycommon.SQL.Add('select * from jtsjk.ftpxxb where domain='''+mynet+'''');
querycommon.Open;
if querycommon.RecordCount>0
then myftp:=querycommon.fieldbyname('ftphost').asstring
else MessageDlg('该网段的FTP主机地址丢失,请通知系统管理员!',mtWarning, [mbOk], 0);
with querycommon do
begin
close;
sql.clear;
sql.Add('select * from jtsjk.BBXXB where mkmc='''+mkmc+''' and beiy1='''+publicform.ywqdm+'''');
open;
if recordcount>0 then
begin
if fieldbyname('zxbb').asstring>bbh then
begin
if (fieldbyname('gxbz').asstring<>'1')and(fieldbyname('gxbz').asstring<>'0')
then begin close;exit;end;
if fieldbyname('gxbz').asstring='1' then
if infoselform.showbox('版本已经更新,是否现在下载新软件?')=false
then begin close;exit;end;
publicform.Tag:=9;
if fieldbyname('gxbz').asstring='0' then
infodlgform.showbox('版本已经更新,现在下载新软件!');
if DirectoryExists(bakdir)=false then MkDir(bakdir);
CopyFile(PChar(initdir+'\'+fieldbyname('ftpfile').asstring),PChar(bakdir+'\'+fieldbyname('ftpfile').asstring),false);
DeleteFile(initdir+'\'+fieldbyname('ftpfile').asstring);
NMFTP1.Host:=myftp;
NMFTP1.UserID:=fieldbyname('ftpuser').asstring;
NMFTP1.Password:=fieldbyname('ftppass').asstring;
try
NMFTP1.Connect;
except
infodlgform.showbox('版本更新时,连接服务器失败!请与市局联系!');
close;
exit;
end;
try
s:=fieldbyname('ftpfile').asstring;
if fileexists(initdir+'\'+s)=true then
begin
if DirectoryExists(bakdir)=false then MkDir(bakdir);
CopyFile(PChar(initdir+'\'+s),PChar(bakdir+'\'+s),false);
if deletefile(initdir+'\'+s)=false then
begin
infodlgform.showbox('请退出所有程序,并手工更新软件!');
close;
exit;
end;
end;
if DirectoryExists(initdir)=false then MkDir(initdir);
NMFTP1.ChangeDir(fieldbyname('ftpdir').asstring);
NMFTP1.Mode(mode_image);
NMFTP1.Download(s,initdir+'\'+s);
NMFTP1.Disconnect;
except
infodlgform.showbox('版本更新时,取文件失败!请与省局联系!');
close;
exit;
end;
infodlgform.showbox('新软件已下载!,请退出并运行文件'+initdir+'\'+s);
close;
close;
sql.clear;
sql.add('update ywsjk.YW_DLJLB set dlbz=:dlbz, out_sj= :out_sj where dlgh=:dlgh and dlbz=:dlbz2');
parambyname('dlgh').asstring:=publicform.czygh;
parambyname('out_sj').asdatetime:=now;
parambyname('dlbz').asstring:='2';
parambyname('dlbz2').asstring:='1';
execsql;
winexec(pchar(initdir+'\'+s),sw_normal);
application.terminate;
end;
close;
exit;
end;
close;
end;
end;
uses
UrlMonfunction DownloadFile(Source, Dest: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
except
Result := False;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if DownloadFile(edit1.Text, edit2.Text) then
ShowMessage('下载成功')
else ShowMessage('下载失败');
end;
uses
UrlMonfunction DownloadFile(Source, Dest: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
except
Result := False;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if DownloadFile(edit1.Text, edit2.Text) then
ShowMessage('下载成功')
else ShowMessage('下载失败');
end;
-----------------------------------------------------------
但是只能下载一个文件~下载多个文件就不能编译了~编译时报错