20. august 2000 - 16:16Der er
10 kommentarer og 2 løsninger
Omskrivning af kode!!
Hvordan omskriver jeg denne kode, til at kunde liste roden fra en ftp server??
Jeg bruger komandoen FTP1.List
Og OnListItem i NMFTP skal koden, som viser rod filerne i en ftp dir.
Har ingen ide om hvordan dette gøres!!
-- Kode start --
Var Fsr : TSearchRec; SogSti : String; ListItem: TListItem; Sti : String; Begin Sti := (\'c:\\\'); If Sti[Length(Sti)] = \'\\\' then else Sti := Sti + \'\\\'; If LastDelimiter(\'g\',Sti) > 0 then else SogSti := Sti + \'*.*\';
If FindFirst(SogSti, faAnyFile, Fsr) = 0 then If FSr.Attr <> faDirectory then Begin ListItem := Server.Items.Add; ListItem.Caption := Fsr.Name; ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle. ListItem.SubItems.Add(IntToStr(Fsr.Size)); end;
While FindNext(Fsr) = 0 do If FSr.Attr <> faDirectory then Begin ListItem := Server.Items.Add; ListItem.Caption := Fsr.Name; ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle. ListItem.SubItems.Add(IntToStr(Fsr.Size)); end; FindClose(Fsr); end;
To recreate this example, you will need to create a new blank Delphi application.
Place 3 TEdits, 9 TButtons, 2 TMemos, a TStatusBar, a TRadioGroup, and a TNMFTP on the form.
Component Descriptions:
Edit1: Host Edit2: User ID Edit3: Password Button1: Connect/Disconnect Button2: NList Button3: ChangeDir Button4: Download Button5: Upload Button6: UploadAppend Button7: UploadUnique Button8: Upload Restore Button9: Abort Memo1: Status Window Memo2: Directory List display RadioGroup1: Transfer Mode * StatusBar1: File Transfer progress
* Change RadioGroup1.Items to MODE_ASCCI, MODE_IMAGE, and MODE_BYTE, in that order.
Insert the following code into Button1\'s OnClick event:
procedure TForm1.Button1Click(Sender: TObject); begin if NMFTP1.Connected then NMFTP1.Disconnect else begin NMFTP1.Host := Edit1.Text; NMFTP1.UserID := Edit2.Text; NMFTP1.Password := Edit3.Text; NMFTP1.Connect; end; end;
When Button1 is clicked, if the Connected property is TRUE, a connection is present, and the Disconnect method is called to disconnect from the remote FTP host. If Connected is FALSE, there is no connection present, so the Host property is set to the value in Edit1 to specify the remote host to connect to. The UserID property is set to the value in Edit2, and the Password property is set to the value in Edit3. The UserID and Password are used to log in to the remote host when connected.
Insert the following code into Button2\'s OnClick event:
procedure TForm1.Button2Click(Sender: TObject); begin Memo2.Clear; NMFTP1.NList; end;
When Button2 is clicked, any text being displayed in Memo2 is cleared. The NList method is called, retrieving the names of all files and directories in the current working directory. The results of this method call can be seen in the OnListItem event.
Insert the following code into Button3\'s OnClick event:
procedure TForm1.Button3Click(Sender: TObject); var TheDir: String; begin if InputQuery(\'Change directory\', \'Directory name?\', TheDir) then
NMFTP1.ChangeDir(TheDir); end;
When Button3 is clicked, the InputQuery function is used to obtain the name of the directory desired. If the Ok button is clicked, the ChangeDir method is used to change the current working directory to the directory specified.
Insert the following code into Button4\'s OnClick event:
procedure TForm1.Button4Click(Sender: TObject); var RemoteFile, LocalFile: String; O: TOpenDialog; begin if InputQuery(\'Download a file\', \'File to download: \', RemoteFile) then begin O := TOpenDialog.Create(Self); try O.Title := \'Save file as\'; if O.Execute then
begin LocalFile := O.FileName; case RadioGroup1.ItemIndex of 0: NMFTP1.Mode(MODE_ASCII); 1: NMFTP1.Mode(MODE_IMAGE); 2: NMFTP1.Mode(MODE_BYTE); end; NMFTP1.Download(RemoteFile, LocalFile); end; finally O.Free; end; end; end;
When Button4 is clicked, the InputQuery function is used to retrieve the name of a remote file to download to the local computer. if the Ok button is clicked, an OpenDialog is used to retrieve the name to store the file as on the local computer. If the Open button is clicked, the Mode method is called, changing the transfer mode depending on the selected item in RadioGroup1. Then the Download method is used to download the remote file to the local disk.
Insert the following code into NMFTP1\'s OnDisconnect event:
procedure TForm1.NMFTP1Disconnect(Sender: TObject); begin Memo1.Lines.Add(\'Disconnected\'); Button1.Caption := \'Connect\'; end;
When the local computer disconnects from the remote host, the OnDisconnect event is called. In this instance, the status display (Memo1) is updated to display the disconnect status, and the caption of Button1 is changed to Connect (Since Button1 is used for connecting and disconnecting).
Insert the following code into NMFTP1\'s OnPacketRecvd event:
procedure TForm1.NMFTP1PacketRecvd(Sender: TObject); begin StatusBar1.SimpleText := IntToStr(NMFTP1.BytesRecvd)+\' bytes received out of \'+IntToStr(NMFTP1.BytesTotal);
end;
When data is received from the remote host during a file transfer, the OnPacketRecvd event is called. In this instance, StatusBar1 displays the progress of the transfer by displaying the BytesRecvd property, which contains the number of bytes received, and the BytesTotal property, which contains the total number of bytes to transfer.
Insert the following code into NMFTP1\'s OnPacketSent event:
procedure TForm1.NMFTP1PacketSent(Sender: TObject); begin StatusBar1.SimpleText := IntToStr(NMFTP1.BytesSent)+\' bytes sent out of \'+IntToStr(NMFTP1.BytesTotal); end;
When data is sent to the remote host during a file transfer, the OnPacketSent event is called. In this instance, StatusBar1 displays the progress of the transfer by displaying the BytesSent property, which contains the number of bytes sent, and the BytesTotal property, which contains the total number of bytes to transfer.
Insert the following code into NMFTP1\'s OnTransactionStart event:
procedure TForm1.NMFTP1TransactionStart(Sender: TObject); begin Memo1.Lines.Add(\'Transaction Start\'); end;
When a data transfer begins, the OnTransactionStart event is called. In this instance, the Status Memo, Memo1, displays that the transaction started.
Insert the following code into NMFTP1\'s OnTransactionStop event:
procedure TForm1.NMFTP1TransactionStop(Sender: TObject); begin Memo1.Lines.Add(\'Transaction Stop\'); end;
When a data transfer completes, the OnTransactionStop event is called. In this instance, the Status Memo, Memo1, displays that the transaction has finished.
Insert the following code into Button5\'s OnClick event:
procedure TForm1.Button5Click(Sender: TObject); var LocalFile, RemoteFile: String; F: File of Byte; FSize: Integer; O: TOpenDialog; begin O := TOpenDialog.Create(Self); try O.Title := \'Select file to upload\'; if O.Execute then if InputQuery(\'Choose Remote File Name\', \'Filename?\', RemoteFile) then begin LocalFile := O.FileName; case RadioGroup1.ItemIndex of 0: NMFTP1.Mode(MODE_ASCII); 1: NMFTP1.Mode(MODE_IMAGE); 2: NMFTP1.Mode(MODE_BYTE); end; AssignFile(F, LocalFile); Reset(F); FSize := FileSize(F); CloseFile(F); NMFTP1.Allocate(FSize);
When Button5 is clicked, an Open Dialog is displayed to select a file on the local computer to upload to the remote host. If the Open button is clicked, the InputQuery function is used to retrieve the name the file will be stored as on the remote host. If the Ok button is clicked, the file is opened so it\'s size can be determined. The Allocate method is called to reserve space on the remote host for the file. This is not normally necessary, but is done here to illustrate the use of the method. The Upload method is used to send the file to the remote host.
Insert the following code into Button6\'s OnClick event:
procedure TForm1.Button6Click(Sender: TObject); var LocalFile, RemoteFile: String; O: TOpenDialog; begin O := TOpenDialog.Create(Self); try O.Title := \'Select file to upload\'; if O.Execute then if InputQuery(\'Choose Remote File Name\', \'Filename?\', RemoteFile) then begin LocalFile := O.FileName; case RadioGroup1.ItemIndex of 0: NMFTP1.Mode(MODE_ASCII); 1: NMFTP1.Mode(MODE_IMAGE); 2: NMFTP1.Mode(MODE_BYTE); end; NMFTP1.UploadAppend(LocalFile, RemoteFile); end; finally O.Free; end; end;
When Button6 is clicked, an Open Dialog is displayed to select a file on the local computer to upload to the remote host. If the Open button is clicked, the InputQuery function is used to retrieve the name the file will be stored as on the remote host. If the Ok button is clicked, the UploadAppend method is used to send the file to the remote host. If a file with the same name already exists on the remote host, the file being sent is appended to the end of the existing file.
Insert the following code into Button7\'s OnClick event:
procedure TForm1.Button7Click(Sender: TObject); var LocalFile: String; O: TOpenDialog; begin O := TOpenDialog.Create(Self); try O.Title := \'Upload file\'; if O.Execute then begin LocalFile := O.FileName; NMFTP1.UploadUnique(LocalFile); end; finally O.Free; end; end;
When Button7 is clicked, an Open Dialog is displayed to select the file on the local computer to upload to the remote host. If the Open button is clicked, the UploadUnique method is used to store the file on the remote FTP host with a unique filename given by the FTP host.
Insert the following code into NMFTP1\'s OnFailure event:
When an FTP command fails, the OnFailure event is called. In this case, the Handled parameter is left the default (FALSE), so an exception will be raised, in addition to a failure notification being added to the status display (Memo1) noting the command that failed.
Insert the following code into NMFTP1\'s OnSuccess event:
When an FTP command succeeds, the OnSuccess event is called. In this instance, the command that succeeded is displayed in the status display (Memo1).
Insert the following code into Button8\'s OnClick event:
procedure TForm1.Button8Click(Sender: TObject); var LocalFile, RemoteFile: String; FPosition: Integer; FPos: String; O: TOpenDialog; begin O := TOpenDialog.Create(Self); try O.Title := \'Select file to upload\'; if O.Execute then if InputQuery(\'Choose Remote File Name\', \'Filename?\', RemoteFile) then if InputQuery(\'Choose restoration point\', \'Byte Count: \', FPos) then begin FPosition := StrToInt(Fpos); LocalFile := O.FileName; case RadioGroup1.ItemIndex of 0: NMFTP1.Mode(MODE_ASCII); 1: NMFTP1.Mode(MODE_IMAGE); 2: NMFTP1.Mode(MODE_BYTE);
When Button8 is clicked, an OpenDialog is displayed to select a file on the local computer to upload to the remote host. If the Open button is clicked, the InputQuery function is used to retrieve the name the file will use on the remote host. The InputQuery function is called once more to obtain the position to restore the upload from. If the Ok button is clicked, the Mode method is used to set the mode as determined by RadioGroup1. The UploadRestore method continues a previously interrupted upload at the position specified.
Insert the following code into Button9\'s OnClick event:
procedure TForm1.Button9Click(Sender: TObject); begin NMFTP1.Abort; end;
When Button9 is clicked, the Abort method is called, aborting the current operation.
Snowball Jeg får stadig fejl i case comandoen!! Jeg bruger også lisview istedet for memo. Det skal bruges til listning af fileren på frp serveren. Ikke bare skrive, hvad der sker!! Se oxo såm http://www.eksperten.dk/spm/20219 Hvis det kan hjælpe.
Jeg har forsøgt at bruge noget af min genialitet (Fnis) ok er kommet frem til at dette her måske er tæt på. Men der sket ikke noget i min Listviev komponent!! -- Kode start --
procedure TForm1.Button5Click(Sender: TObject); begin Server.Items.Clear; FTP1.List; end;
procedure TForm1.FTP1ListItem(Listing: String); Var Fsr : TSearchRec; SogSti : String; ListItem: TListItem; Sti : String; Begin Sti := (Listing); If Sti[Length(Sti)] = \'\\\' then else Sti := Sti + \'\\\'; If LastDelimiter(\'g\',Sti) > 0 then else SogSti := Sti + \'*.*\';
If FindFirst(SogSti, faAnyFile, Fsr) = 0 then If FSr.Attr <> faDirectory then Begin ListItem := Server.Items.Add; ListItem.Caption := Fsr.Name; ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle. ListItem.SubItems.Add(IntToStr(Fsr.Size)); end;
While FindNext(Fsr) = 0 do If FSr.Attr <> faDirectory then Begin ListItem := Server.Items.Add; ListItem.Caption := Fsr.Name; ListItem.SubItems.Add(ExtractFileExt(Fsr.Name)); //Denne linie tilføjer efternavnet i den næste celle. ListItem.SubItems.Add(IntToStr(Fsr.Size)); end; FindClose(Fsr); end;
Tager point igen, da det ikek ser ud til at jeg får besvaret mit spm.
Elders tak for hjælpen Snowball
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.