FTPClientUnit.pas
上传用户:yzhui7711
上传日期:2021-12-23
资源大小:293k
文件大小:3k
源码类别:

Ftp客户端

开发平台:

Delphi

  1. unit FTPClientUnit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, ExtCtrls, ComCtrls, Menus, StdCtrls, ShellAPI, FileListExplorer,
  6.   CustomFileSystem, FTPFileSystem, FolderTreeExplorer, LocalFileSystem,
  7.   FileSystemBrowseDialog, FileSystemSaveDialog, FTPLoginDialog;
  8. type
  9.   TFTPClientForm = class(TForm)
  10.     FolderTreeExplorer1: TFolderTreeExplorer;
  11.     FileListExplorer1: TFileListExplorer;
  12.     FTPFileSystem1: TFTPFileSystem;
  13.     Splitter1: TSplitter;
  14.     MainMenu1: TMainMenu;
  15.     File1: TMenuItem;
  16.     Connect1: TMenuItem;
  17.     Disconnect1: TMenuItem;
  18.     StatusBar1: TStatusBar;
  19.     PopupMenu1: TPopupMenu;
  20.     DownloadFile1: TMenuItem;
  21.     Openfile1: TMenuItem;
  22.     LocalFileSystem1: TLocalFileSystem;
  23.     FileSystemSaveDialog1: TFileSystemSaveDialog;
  24.     FTPLoginDialog1: TFTPLoginDialog;
  25.     procedure Connect1Click(Sender: TObject);
  26.     procedure Disconnect1Click(Sender: TObject);
  27.     procedure FTPFileSystem1Echo(Sender: TObject; EchoStr: String);
  28.     procedure PopupMenu1Popup(Sender: TObject);
  29.     procedure DownloadFile1Click(Sender: TObject);
  30.     procedure FormCreate(Sender: TObject);
  31.     procedure Openfile1Click(Sender: TObject);
  32.     procedure FileListExplorer1DblClick(Sender: TObject);
  33.   private
  34.     { Private declarations }
  35.   public
  36.     { Public declarations }
  37.   end;
  38. var
  39.   FTPClientForm: TFTPClientForm;
  40. implementation
  41. {$R *.dfm}
  42. procedure TFTPClientForm.Connect1Click(Sender: TObject);
  43. begin
  44.   if not FTPFileSystem1.Connected then
  45.     FTPFileSystem1.Connect;
  46. end;
  47. procedure TFTPClientForm.Disconnect1Click(Sender: TObject);
  48. begin
  49.   if FTPFileSystem1.Connected then
  50.     FTPFileSystem1.DisConnect;
  51. end;
  52. procedure TFTPClientForm.FTPFileSystem1Echo(Sender: TObject;
  53.   EchoStr: String);
  54. begin
  55.   StatusBar1.SimpleText:=EchoStr;
  56. end;
  57. procedure TFTPClientForm.PopupMenu1Popup(Sender: TObject);
  58. begin
  59.   DownloadFile1.Enabled:=(FileListExplorer1.SelCount=1) and not FileListExplorer1.Selected.Descriptor.Folder;
  60.   Openfile1.Enabled:=DownloadFile1.Enabled;
  61. end;
  62. procedure TFTPClientForm.DownloadFile1Click(Sender: TObject);
  63. var
  64.   f:TFileStream;
  65.   s:string;
  66. begin
  67.   with FileSystemSaveDialog1 do
  68.     if Execute then begin
  69.       s:=CurrentDirectory+FileName;
  70.       s:=Copy(s,2,Length(s)-1);
  71.       f:=TFileStream.Create(s,fmOpenWrite or fmCreate);
  72.       f.Size:=0;
  73.       FTPFileSystem1.ReadStreamFromFile(FileListExplorer1.Selected.Descriptor.FilePath+FileListExplorer1.Selected.Descriptor.FileName,f);
  74.       f.Destroy;
  75.     end;
  76. end;
  77. procedure TFTPClientForm.FormCreate(Sender: TObject);
  78. begin
  79.   PopupMenu1.Items.Caption:='File';
  80. end;
  81. procedure TFTPClientForm.Openfile1Click(Sender: TObject);
  82. var
  83.   f:TFileStream;
  84.   s:array[0..MAX_PATH] of Char;
  85.   t:string;
  86.   SEI:TShellExecuteInfo;
  87. begin
  88.   GetTempPath(MAX_PATH,@s[0]);
  89.   t:=s+FileListExplorer1.Selected.Descriptor.FileName;
  90.   f:=TFileStream.Create(t,fmOpenWrite or fmCreate);
  91.   f.Size:=0;
  92.   FTPFileSystem1.ReadStreamFromFile(FileListExplorer1.Selected.Descriptor.FilePath+FileListExplorer1.Selected.Descriptor.FileName,f);
  93.   f.Destroy;
  94.   ZeroMemory(@SEI,SizeOf(SEI));
  95.   SEI.cbSize:=SizeOf(SEI);
  96.   SEI.lpFile:=PChar(t);
  97.   SEI.nShow:=SW_SHOWNORMAL;
  98.   Assert(ShellExecuteEx(@SEI),SysErrorMessage(GetLastError));
  99. end;
  100. procedure TFTPClientForm.FileListExplorer1DblClick(Sender: TObject);
  101. begin
  102.   if (FileListExplorer1.SelCount=1) and not FileListExplorer1.Selected.Descriptor.Folder then
  103.     Openfile1Click(nil);
  104. end;
  105. end.