asp.net tidftp Usage Introduction _ Practical Skills
Source: Internet
Author: User
1 connecting to a remote server
Procedure Connect (Aautologin:boolean; const atimeout:integer);
2 Change Directory
Procedure Changedir (const adirname:string);
3 download
Procedure get (const asourcefile:string; Adest:tstream; Aresume:boolean); overload;
Procedure get (const asourcefile:string; const adestfile:string; const Acanoverwrite:boolean; Aresume:boolean); overload;
4 Upload
Procedure put (const asource:tstream; const adestfile:string; const Aappend:boolean); overload;
Procedure put (const asourcefile:string; const adestfile:string; const Aappend:boolean); overload;
5 Delete
Procedure Delete (const afilename:string);
Determining whether to connect
If Idftp1.connected Then
Begin
...........
End
Quote someone for a second, make your own notes later
Now a lot of applications need to upload and download large files, HTTP way to upload large files have certain limitations. Fortunately, ftp as a very old and very mature protocol can efficiently and stably complete the upload of large files, and can be perfect for the continuation of the transmission. Just take the movie I wrote. Server Management program, after the comparison of various schemes, found that the use of FTP can be perfect to achieve the requirements. But to achieve FTP through the WinSocket library is a hassle, but fortunately there is a indy--package that wraps most of the network protocols.
By Indy, programmers can program in a blocking way, leaving out crappy winsocket asynchronous patterns and using the same blocking programming pattern as UNIX systems. In this way, the programmer can well handle the running process of the program.
Below, we enter into the TIDFTP world of Indy.
1. Description of the control
Using the Tidftp control in Indy 9, you can upload and download files via FTP.
2. The specific use of the control
(1) Control property settings
Default properties, properties that are directly related to server connections, such as host names and users, are set when a connection is established. The values for the Recvbuffersize and sendbuffersize two properties need to be set. In addition, you need to specify the TransferType attribute based on the file type you want to transfer, and the other properties are set by default values.
Recvbuffersize description (Default is 8192 bytes): This property is an integer variable that specifies the size of the accept buffer used for the connection.
Sendbuffersize description (Default is 32768 bytes): This property is also an integer variable that specifies the maximum value of the send buffer used by the connection. When the property is in the Writestream method, it can be used to tstream specify the number of blocks to send the content. If the content to be sent is greater than the value of this property, the sending content is divided into multiple blocks to send.
TransferType description (Default is Ftbinary): This property is a tidftptransfertype type variable. Used to specify whether the transport content is binary (ftbinary) or ASCII (FTASCII). Applications need to transfer executables, compress files, and multimedia files in binary mode, and use ASCII to transmit text-type data, such as text or hypertext.
(2) Event response for the control
Ondisconnected Response: Tnotifyevent class for responding to a disconnected (disconnect) event. The response is triggered when the Disconnect method is invoked to close the socket. The application must specify the procedure for the event response to be appropriate for the Disconnect event.
Onstatus Response: Tidstatusevent class. The response is triggered when the state of the current connection changes. This event can be triggered by the Dostatus method and supplied to the event controller properties. Axstatus is the Tidstatus value of the current connection; Aaargs is an optional parameter for formatting functions that will be used to construct text messages that represent the state of the current connection.
Onwork response: Onword is the response controller for Tworkevent class events. Onwork is used to associate DoWork methods to notify Indy components and classes when a buffer read-write operation is invoked. It is generally used to control the updates of progress bars and window elements. Aworkmode represents the mode of the current operation where the wmread-component is reading the data, and the wmwrite-component is sending the data. Aworkcount indicates the byte count of the current operation.
Onworkbegin Response: Tworkbeginevent class. When a buffer read-write operation initializes, the event association Beginwork method is used to notify Indy components and classes. It is generally used to control the updates of progress bars and window elements. Aworkmode represents the mode of the current operation where the wmread-component is reading the data, and the wmwrite-component is sending the data. Aworkcountmax is used to indicate the maximum number of bytes for an operation sent to the Onworkbegin event, and a value of 0 represents the unknown.
Onworkend Response: Tworkendevent class. When a buffer read-write operation terminates, the event association Endwork method is used to notify Indy components and classes. Aworkmode represents the mode of the current operation where the wmread-component is reading the data, and the wmwrite-component is sending the data. Aworkcount represents the number of bytes in the operation.
In the event response, it is mainly through the above five kinds of event response to control program. In general, set the interface notification in the ondisconnected, set the state of the current operation in the Onstatus, and implement the display of status bars and other parameters in the onwork. , and in Onworkbegin and Onworkend, the interface between the beginning of transmission and the end of transmission is set respectively.
(3) Connect to remote server
After you have finished setting the control properties and implementing the control's event response, you can interact and transfer with the server. Before connecting, you should first determine whether IDFTP is connected and, if connected is False, specify the settings for some TCP class properties related to the server connection through an interface control or otherwise: host (host name): String, Username (username): string, Password (password): string, or you can specify port. The Connect method is then invoked to connect to the remote server, and if no exception occurs, the connection is successfully established.
Process Description: Procedure Connect (Aautologin:boolean; const atimeout:integer);
This procedure connects to a remote FTP server
attribute: Aautologin:boolean = True
Automatically log on after connection, this parameter defaults to TRUE.
Const Atimeout:integer = Idtimeoutdefault
Timeout, in units: seconds.
Sample code:
If Idftp1.connected then try
If Transferrigndata then Idftp1.abort;
Idftp1.quit;
Finally
End
else with IdFTP1 do try
Username: = Useridedit.text;
Password: = Passwordedit.text;
Host: = Ftpserveredit.text;
Connect;
Changedir (Currentdiredit.text);
Finally
End
(4) Change the catalogue
After the connection is established, you can change the directory in which the current FTP session resides. For known absolute paths, you can directly invoke the Changedir (const adirname:string) method to convert the directory, adirname represent the file system directory on the server, and you can also call Changedirup back to the parent directory.
If the path is unknown, the current directory structure of the remote server can be obtained through the list (adest:tstrings const aspecifier:string const Adetails:boolean) procedure. You must set TransferType to FTASCII (ASCII mode), where: Adest saves the current directory structure and can call the list in a subsequent program. You can also get the current directory name by using the Retrievecurrentdir method.
Process Description:
Procedure Changedir (const adirname:string);
Change Working directory
Property
Const adirname:string
Directory description for remote server
Description: This process is actually implementing the FTP CWD command.
Procedure Changedirup;
To the previous level directory
function retrievecurrentdir:string;
This function returns the current directory name
Procedure List (adest:tstrings; const aspecifier:string; const Adetails:boolean);
Lists all files and subdirectories and their properties for the current directory
Parameters:
Adest:tstrings
Save file and subdirectory return results
Const ASPECIFIER:STRING =
A file mask used to list eligible files
Const Adetails:boolean = True
Include File and subdirectory properties
Property directorylisting:tidftplistitems;
Returns a list of files and directory structures
Sample code:
LS: = tstringlist.create;
Try
Idftp1.changedir (dirname);
Idftp1.transfertype: = Ftascii;
Currentdiredit.text: = Idftp1.retrievecurrentdir;
DirectoryListBox.Items.Clear;
Idftp1.list (LS);
DirectoryListBox.Items.Assign (LS);
If DirectoryListBox.Items.Count > 0 Then
If Ansipos (total, directorylistbox.items[0]) > 0 Then DirectoryListBox.Items.Delete (0);
Finally
Ls. Free;
End
(5) Implementation of the download
Before downloading, you must view Directorylisting.items[scurrfile]. Whether the itemtype is a file, such as a return to Ditdirectory, represents the current file name as a directory, cannot be downloaded, must be directed to a file. If you are a file, you can download it. Before downloading, set the type of transfer as a binary file, and specify the path to be saved locally. The download of the file is implemented by calling the Get method. The download process is slow, and you can consider putting it into a thread to implement it.
Process Description:
Procedure get (const asourcefile:string; Adest:tstream; Aresume:boolean); overload;
Procedure get (const asourcefile:string; const adestfile:string; const Acanoverwrite:boolean; Aresume:boolean); overload;
Gets the file from the remote server.
Attribute Description:
Const asourcefile:string
Source file name on the remote server
Const adestfile:string
File name saved to the client
Const Acanoverwrite:boolean = False
Overwrite file with same name
Aresume:boolean = False
Do you want to continue the breakpoint?
Sample code:
Savedialog1.filename: = Name;
If Savedialog1.execute then BEGIN
Setfunctionbuttons (FALSE);
Idftp1.transfertype: = ftbinary;
Bytestotransfer: = Idftp1.size (Name);
If FileExists (Name) THEN BEGIN
Case Messagedlg (File aready exists. Do your want to resume the download operation?
Delete Files use the Delete method, which deletes the specified file, and the deleted object must be a file. If you want to delete a directory, use the RemoveDir method.
Process Description:
Procedure Delete (const afilename:string);
deleting files
Procedure RemoveDir (const adirname:string);
Deleting a folder has different requirements depending on the server's deletion of the folder. Some servers do not allow the deletion of Non-empty folders, and programmers need to add code to empty directories.
The parameters for both of these procedures are the target names
If Idftp1.directorylisting.items[icurrselect]. ItemType = Ditdirectory Then try
IDFTP1. RemoveDir (Name);
Finally
End
Else
Try
IDFTP1. Delete (Name);
Finally
End
(8) The realization of the back
Backward in fact is a directory operation of one, can simply change the current directory for ... , or it can be achieved by going back to the parent directory.
(9) Cancellation of the implementation
You can cancel the current operation at any time by using the Abort method during the idftp transfer process. You can onwork the implementation of the event to determine when to cancel the operation.
(10) The realization of the continuation of the breakpoint
The continuation of a breakpoint is the beginning of the upload or download process to determine whether the transmitted files have been transmitted over, if the transmission did not complete successfully, the last interrupt to continue the transfer work. It takes two important actions to implement this function, first, to determine the size of the file, and then to specify the behavior of the upload in the transfer process get and put.
Determine the size of the file on the server using the function size (FileName). During the download process, compare the information for local and remote files, and then specify aresume: = True in Get. and upload is the same, specify put aappend: = true on it.
As we said earlier, Indy's network operations are mostly blocking mode, and Tidftp is no exception. This allows the user interface to be temporarily frozen during the operation of each of these operations, and must wait for the call to return to continue the user action interface response. So in the actual programming, we need to use multithreading to ensure the response of the user interface. Windows systems can use CreateThread system calls to create threads, but there are a lot of extra work that developers need to do to keep threads synchronized during use. And Indy also contains the implementation of multi-threaded control tidthreadcomponent, in contrast to the control to achieve multiple threads more convenient and easier to control. I will introduce the use of tidthreadcomponent in the following articles.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service