如何用Indy元件,在自寫的ftp程式中上傳檔案? |
答題得分者是:cmf
|
annie
一般會員 發表:5 回覆:6 積分:2 註冊:2002-12-22 發送簡訊給我 |
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
引言: 請問:如果使用Indy元件寫了一個簡單的ftp程式,若要上傳檔案該如何寫?// 檔案傳輸協定 FTP TIdFTP = class(TIdTCPClient) TIdFtp 檔案傳輸協定 工具 FTP 是 基本 檔案分享系統 提供 上傳 及 下載 檔案 且可以管理檔案及目錄 類別繼承關希 TComponent -> TidBasComponent -> TidComponent-> TidTCPComponent-> TidCPConnection -> TIdTCPClient -> TIdFTP TIdFTP 屬性參數 Passive = Determines the FTP data connection method. Password= Password for FTP Login. Port=Port number used for the connection. Port is an Integer property that identifies the server port number server used for the client connection. By convention, some internet protocols reserve port number including: 21 - FTP 25 - SMTP 80 - HTTP 119 - NNTP SystemDesc=Description of the FTP server. TransferType=Indicates the file transfer type. TIdFTPTransferType = (ftBinary, ftASCII); User=User name for login to the FTP server. BoundIP=Specifies the IP address to use for the client. Host=Address of the remote computer system. OnConnected=Specifies a connection event handler. SocksInfo=SOCKS proxy configuration. UseNagle=Enable or disable the Nagle algorithm. ASCIIFilter=Converts incoming characters to ASCII. Binding=Represents the socket handle for the connection. ClosedGracefully=Indicates that the connection has closed without data in the read buffer. CmdResult=Represents a response message. CmdResultDetails=Represents a group of response messages. Intercept=Allows intercept of read and write operations for the connection. InterceptEnabled=Indicates the active state of the Intercept. OnDisconnected=Represents the event handler for peer disconnect events. OnWork=Work event handler for buffered read and write operations. OnWorkBegin=WorkBegin event handler for starting read or write buffering. OnWorkEnd=WorkEnd event handler triggered when stopping read and write buffering operations. ReadLnTimedOut=Indicates that a timeout occurred in ReadLn. RecvBufferSize=Indicates the current size of the default receive buffer for the connection. ResultNo=Identifies the numeric response code for a command. SendBufferSize=Indicates the maximum buffer size for writing to the connection. LocalName=Name of the local computer. Version=Identifies the Indy version number. TIdFTP 方法 Abort=Stops an FTP service operation. ChangeDir=Changes the current directory on the FTP server. ChangeDirUp=Changes to the parent directory on the FTP server. Connect=Opens a connection to the FTP server. Create=Instantiates a TIdFTP instance. Delete=Remove a file on the FTP server. procedure Get(const ASourceFile: string; ADest: TStream); overload; procedure Get(const ASourceFile: string; const ADestFile: string; const ACanOverwrite: boolean); overload; KillDataChannel=Closes the FTP data channel. List=Gets a list of files and directories on the FTP server. MakeDir=Create a directory on the FTP server. Noop=Send a Keep-Alive signal to the FTP server. procedure Put(const ASource: TStream; const ADestFile: string; const AAppend: boolean); overload; procedure Put(const ASourceFile: string; const ADestFile: string; const AAppend: boolean); overload; Quit=Closes the FTP client connection. RemoveDir=Remove a directory on the FTP server. Rename=Change the name of a file on the FTP server. RetrieveCurrentDir=Gets the current directory name on the FTP server. Site=Sends an FTP server site command. Size=Gets file size information from the FTP server. 下面是 FTP 範例 unit mainf; interface uses {$IFDEF Linux} QGraphics, QControls, QForms, QDialogs, QStdCtrls, QExtCtrls, QComCtrls, QMenus, QTypes, {$ELSE} windows, messages, graphics, controls, forms, dialogs, stdctrls, extctrls, comctrls, menus, {$ENDIF} SysUtils, Classes, IdIntercept, IdLogBase, IdLogDebug, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, IdAntiFreezeBase, IdAntiFreeze; type TMainForm = class(TForm) DirectoryListBox: TListBox; IdFTP1: TIdFTP; IdLogDebug1: TIdLogDebug; DebugListBox: TListBox; Panel1: TPanel; FtpServerEdit: TEdit; ConnectButton: TButton; Splitter1: TSplitter; Label1: TLabel; UploadOpenDialog1: TOpenDialog; Panel3: TPanel; SaveDialog1: TSaveDialog; StatusBar1: TStatusBar; TraceCheckBox: TCheckBox; CommandPanel: TPanel; UploadButton: TButton; AbortButton: TButton; BackButton: TButton; DeleteButton: TButton; DownloadButton: TButton; UserIDEdit: TEdit; PasswordEdit: TEdit; Label2: TLabel; Label3: TLabel; IdAntiFreeze1: TIdAntiFreeze; ProgressBar1: TProgressBar; UsePassive: TCheckBox; CurrentDirEdit: TEdit; ChDirButton: TButton; CreateDirButton: TButton; PopupMenu1: TPopupMenu; Download1: TMenuItem; Upload1: TMenuItem; Delete1: TMenuItem; N1: TMenuItem; Back1: TMenuItem; procedure ConnectButtonClick(Sender: TObject); procedure IdLogDebug1LogItem(ASender: TComponent; var AText: String); procedure UploadButtonClick(Sender: TObject); procedure DirectoryListBoxDblClick(Sender: TObject); procedure DeleteButtonClick(Sender: TObject); procedure IdFTP1Disconnected(Sender: TObject); procedure AbortButtonClick(Sender: TObject); procedure BackButtonClick(Sender: TObject); procedure IdFTP1Status(axSender: TObject; const axStatus: TIdStatus; const asStatusText: String); procedure TraceCheckBoxClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure DirectoryListBoxClick(Sender: TObject); procedure IdFTP1Work(Sender: TObject; AWorkMode: TWorkMode; const AWorkCount: Integer); procedure IdFTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode; const AWorkCountMax: Integer); procedure IdFTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode); procedure UsePassiveClick(Sender: TObject); procedure ChDirButtonClick(Sender: TObject); procedure CreateDirButtonClick(Sender: TObject); private { Private declarations } AbortTransfer: Boolean; TransferrignData: Boolean; BytesToTransfer: LongWord; STime: TDateTime; procedure ChageDir(DirName: String); procedure SetFunctionButtons(AValue: Boolean); procedure SaveFTPHostInfo(Datatext, header: String); function GetHostInfo(header: String): String; public { Public declarations } end; var MainForm: TMainForm; implementation {$IFDEF MSWINDOWS}{$R *.dfm}{$ELSE}{$R *.xfm}{$ENDIF} Uses IniFiles; Var AverageSpeed: Double = 0; procedure TMainForm.SetFunctionButtons(AValue: Boolean); Var i: Integer; begin with CommandPanel do for i := 0 to ControlCount - 1 do if Controls[i].Name <> 'AbortButton' then Controls[i].Enabled := AValue; with PopupMenu1 do for i := 0 to Items.Count - 1 do Items[i].Enabled := AValue; ChDirButton.Enabled := AValue; CreateDirButton.Enabled := AValue; end; procedure TMainForm.ConnectButtonClick(Sender: TObject); begin ConnectButton.Enabled := false; if IdFTP1.Connected then try if TransferrignData then IdFTP1.Abort; IdFTP1.Quit; finally //Panel3.Caption := 'Current directory is: '; CurrentDirEdit.Text := '/'; DirectoryListBox.Items.Clear; SetFunctionButtons(false); ConnectButton.Caption := 'Connect'; ConnectButton.Enabled := true; ConnectButton.Default := true; end else with IdFTP1 do try User := UserIDEdit.Text; Password := PasswordEdit.Text; Host := FtpServerEdit.Text; Connect; Self.ChageDir(CurrentDirEdit.Text); SetFunctionButtons(true); SaveFTPHostInfo(FtpServerEdit.Text, 'FTPHOST'); finally ConnectButton.Enabled := true; if Connected then begin ConnectButton.Caption := 'Disconnect'; ConnectButton.Default := false; end; end; end; procedure TMainForm.IdLogDebug1LogItem(ASender: TComponent; var AText: String); begin DebugListBox.ItemIndex := DebugListBox.Items.Add(AText); end; procedure TMainForm.UploadButtonClick(Sender: TObject); begin if IdFTP1.Connected then begin if UploadOpenDialog1.Execute then try SetFunctionButtons(false); IdFTP1.TransferType := ftBinary; IdFTP1.Put(UploadOpenDialog1.FileName, ExtractFileName(UploadOpenDialog1.FileName)); ChageDir(idftp1.RetrieveCurrentDir); finally SetFunctionButtons(true); end; end; end; procedure TMainForm.ChageDir(DirName: String); begin try SetFunctionButtons(false); IdFTP1.ChangeDir(DirName); IdFTP1.TransferType := ftASCII; CurrentDirEdit.Text := IdFTP1.RetrieveCurrentDir; {Panel3.Caption := 'Current directory is: ' IdFTP1.RetrieveCurrentDir ' Remote system is ' IdFTP1.SystemDesc;} DirectoryListBox.Items.Clear; IdFTP1.List(DirectoryListBox.Items); finally SetFunctionButtons(true); end; end; function GetNameFromDirLine(Line: String; Var IsDirectory: Boolean): String; Var i: Integer; DosListing: Boolean; begin IsDirectory := Line[1] = 'd'; DosListing := false; for i := 0 to 7 do begin if (i = 2) and not IsDirectory then begin IsDirectory := Copy(Line, 1, Pos(' ', Line) - 1) = '
------
︿︿ |
opksue
一般會員 發表:2 回覆:8 積分:2 註冊:2002-09-07 發送簡訊給我 |
|
opksue
一般會員 發表:2 回覆:8 積分:2 註冊:2002-09-07 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |