判断文件目录名称是否存在
function TUPfileFRM.FtpDirectoryExists(ADir: string): Boolean;
var
index:Integer;
begin
Index:=0;
Result := false;
try
if Assigned(dllUPfileFRM.IdFtp1.DirectoryListing) and (dllUPfileFRM.IdFtp1.DirectoryListing.Count>0) then
while Indexbegin
with dllUPfileFRM.IdFtp1.DirectoryListing.Items[Index] do
begin
if (trim(FileName)=trim(ADir)) and (ItemType = ditDirectory) then 是文件夹类型 编译不通过时要USES IDFTPLIST
begin
Result:=true;
Exit;
end;
end;
Index:=Index+1;
end;
except
Result := False;
end;
end;
调用判断函数的功能:
function TUPfileFRM.Dllupfiles(ASourceFile,ADestFile,sAir:string):boolean;
var
LS: TStringList;
begin
result:=false;
LS:=TStringList.Create;
try
IdFTP1.Host := '192.168.0.1';
IdFTP1.Username := 'username';
IdFTP1.Password := 'pass123';
IdFTP1.TransferType := ftASCII; 编译不通过时 USES IdFTPCommon
if IdFTP1.Connected then IdFTP1.Disconnect;
IdFTP1.Connect();
IdFTP1.List(LS);
if (not FtpDirectoryExists(sAir)) then IdFTP1.MakeDir(sAir);
try
IdFTP1.ChangeDirUp;
IdFTP1.Put (ASourceFile,ADestFile);
showmessage('上传文件成功!');
result:=true;
except
showmessage('上传文件失败');
end;
finally
LS.Free;
end;
end;