asp.net a more complete package of FTP class _ Practical Tips

Source: Internet
Author: User
Tags ftp connection ftp file ftp login ftp file transfer
Let's see the simple one.
Copy Code code as follows:

FTP FTP = new FTP ("192.168.1.117", "Ahuinan", "123456");

String oldname = this. FileUpload1.PostedFile.FileName;
String newname = dd + H + mm + SS + Oldname. Substring (oldname. LastIndexOf ("."));


Ftp.connect ();
Ftp.openupload (Oldname,newname);
while (Ftp.doupload () > 0)
{
int perc = (int) ((ftp.bytestotal) *)/ftp.filesize);
Response.Write (perc. ToString () + "%<br/>");
Response.Flush ();
}


Function

• Create an FTP link

U Support Agent

U multi-overloaded synchronous upload files

U multiple overloaded asynchronous upload files

U multi-overloaded sync download files

Multi-overloaded asynchronous download files

U upload file percent of progress

• The progress of the download file white ratio

You have a differentiated list of directories or file information (taking into account MS DOS display format and UNIX format)

U directory or file existence judgment

U Delete File

• Renaming files, directories

• Copy and move files on the FTP server (many people may be struggling to find this feature)

• Create, delete directory

U directory Switching operation (very novel Oh J)

Public constructors

Name

Description

Clsftp ()

Initializes a new instance of the Clsftp class

Clsftp (Uri Ftpuri, String strUserName, String strpassword)

Initializes a new instance of the Clsftp class by the FTP address, FTP login user information, FTP login user password

Clsftp (Uri Ftpuri, String strUserName, String strpassword, WebProxy objproxy)

New instance of Clsftp class initialized by FTP address, FTP login user information, FTP login user password, Proxy server object

Public properties

/tr>

name

Description

Uri

Get or set FTP server address

Direct Orypath

Get current working directory

UserName

Get or set FTP logon user

Password

Get or set FTP login password

Proxy

Get or set the proxy service to connect to the FTP server

errormsg

Get the drill in return The error message

Public methods

Copy one of the files below the current directory to another directory on the server, and note that after you copy the file, the current working directory or the directory where the file was originally located. Description of the

parameter:

remotefile: File name under current directory

directoryname: New directory name. The

  Description: If the new directory is a subdirectory of the current directory, specify the subdirectory directly. such as: Subdirectory1/subdirectory2

  If the new directory is not a subdirectory of the current directory, you must specify it from the root level. such as:./newdirectory/subdirectory1/subdirectory2

TD valign= "Top" width= "321" >

Name

Description

DownloadFile (String remotefilename, String localpath)

Download the file from the FTP server and save the file with a filename with the same name as the remote file.

Parameter description:

Remotefilename: Remote file name

LocalPath: Local Path

DownloadFile (String remotefilename, String localpath, String localfilename)

Download the file from the FTP server, specifying the local path and local file name.

Parameter description:

LocalPath: Local Path

Localfilepath: Save the local path to the file, followed by "\"

LocalFilename: Save the local file name

DownloadFile (String remotefilename)

Download the file from the FTP server and return the file binary data.

Parameter description:

Remotefilename: Remote file name

DownloadFileAsync (String remotefilename, String localpath, String localfilename)

Asynchronously downloads files from the FTP server, specifying the local path and local file name.

Parameter description:

Remotefilename: Remote file name

LocalPath: Save the local path to the file, followed by "\"

LocalFilename: Save the local file name

DownloadFileAsync (String remotefilename, String localfullpath)

Asynchronously downloads files from the FTP server, specifying the local full path file name.

Parameter description:

Remotefilename: Remote file name

Localfullpath: Local full path file name

UploadFile (String Localfullpath)

Upload files to the FTP server.

Parameter description:

Localfullpath: Local file name with full path

UploadFile (string localfullpath, bool overwriteremotefile)

Upload files to the FTP server.

Parameter description:

Localfullpath: Local file with full path

Overwriteremotefile: Overwrite files with the same name on the remote server

UploadFile (String Localfullpath, String remotefilename)

Upload files to the FTP server.

Parameter description:

Localfullpath: Local file with full path

Remotefilename: To save the file name on the FTP server

UploadFile (String Localfullpath, String remotefilename, bool overwriteremotefile)

Upload files to the FTP server.

Parameter description:

Localfullpath: Local file name with full path

Remotefilename: To save the file name on the FTP server

Overwriteremotefile: Overwrite files with the same name on the remote server

UploadFile (byte[] filebytes, string remotefilename)

Upload files to the FTP server.

Parameter description:

Filebytes: Uploaded binary data

Remotefilename: To save the file name on the FTP server

UploadFile (byte[] filebytes, string remotefilename, bool overwriteremotefile)

Upload files to the FTP server.

Parameter description:

Filebytes: File binary content

Remotefilename: To save the file name on the FTP server

Overwriteremotefile: Overwrite files with the same name on the remote server

Uploadfileasync (String Localfullpath)

Upload files asynchronously to the FTP server.

Parameter description:

Localfullpath: Local file name with full path

Uploadfileasync (string localfullpath, bool overwriteremotefile)

Upload files asynchronously to the FTP server.

Parameter description:

Localfullpath: Local file with full path

Overwriteremotefile: Overwrite files with the same name on the remote server

Uploadfileasync (String Localfullpath, String remotefilename)

Upload files asynchronously to the FTP server.

Parameter description:

Localfullpath: Local file with full path

Remotefilename: To save the file name on the FTP server

Uploadfileasync (String Localfullpath, String remotefilename, bool overwriteremotefile)

Upload files asynchronously to the FTP server.

Parameter description:

Localfullpath: Local file name with full path

Remotefilename: To save the file name on the FTP server

Overwriteremotefile: Overwrite files with the same name on the remote server

Uploadfileasync (byte[] filebytes, string remotefilename)

Upload files asynchronously to the FTP server.

Parameter description:

Filebytes: Uploaded binary data

Remotefilename: To save the file name on the FTP server

Uploadfileasync (byte[] filebytes, string remotefilename, bool overwriteremotefile)

Upload files asynchronously to the FTP server.

Parameter description:

Filebytes: File binary content

Remotefilename: To save the file name on the FTP server

Overwriteremotefile: Overwrite files with the same name on the remote server

Listfilesanddirectories ()

Lists all files and directories for the current directory above the FTP server.

Listfiles ()

Lists all files in the current directory above the FTP server.

Listdirectories ()

Lists all directories for the current directory above the FTP server.

Directoryexist (String remotedirectoryname)

Determines whether a subdirectory specified under the current directory exists.

Parameter description:

Remotedirectoryname: Specified directory name

FileExist (String remotefilename)

Determines whether a remote file exists under the current directory of the server.

Parameter description:

Remotefilename: Remote file name

DeleteFile (String remotefilename)

Deletes a file from the FTP server.

Parameter description:

Remotefilename: Remote file name

ReName (String remotefilename, String newfilename)

Change the name of a file or the name of a directory.

Parameter description:

Remotefilename: Original file or directory name

NewFileName: The name of the new file or directory

Copyfiletoanotherdirectory (string remotefile, String directoryname)

Movefiletoanotherdirectory (String remotefile, String directoryname)

Move one of the files below the current directory to the other directory on the server, and note that after you move the file, the current working directory or the directory where the file is originally located.

Parameter description:

RemoteFile: File name under current directory

DirectoryName: New directory name.

Note: If the new directory is a subdirectory of the current directory, specify the subdirectory directly. such as: Subdirectory1/subdirectory2;

If the new directory is not a subdirectory of the current directory, you must specify one level from the root directory. For example:./newdirectory/subdirectory1/subdirectory2

Makedirectory (String directoryname)

Create a subdirectory on the FTP server for the current working directory.

Parameter description:

DirectoryName: Sub Directory Name

RemoveDirectory (String directoryname)

Deletes a subdirectory from the current working directory.

Parameter description:

DirectoryName: Sub Directory Name

Isvalidpathchars (String directoryname)

Determines whether the characters in the directory name are legitimate.

Parameter description:

DirectoryName: Directory Name

Isvalidfilechars (String FileName)

Determines whether the characters in the file name are legitimate.

Parameter description:

FileName: File name

Gotodirectory (String directoryname)

Enter a directory.

Parameter description:

DirectoryName:

The name of the new catalog.

Note: If the new directory is a subdirectory of the current directory, specify the subdirectory directly. such as: Subdirectory1/subdirectory2;

If the new directory is not a subdirectory of the current directory, you must specify one level from the root directory. For example:./newdirectory/subdirectory1/subdirectory2

Enteronesubdirectory (String directoryname)

Enter a subdirectory from the current working directory.

Parameter description:

DirectoryName: Sub Directory Name

Comeoutdirectory ()

Go to the previous directory from the current working directory.

Public events

Name

Description

DownloadProgressChanged

Asynchronous download progress changes triggered events

downloaddatacompleted

Events triggered after the asynchronous download file completes

Uploadprogresschanged

Asynchronous upload progress changes triggered events

uploadfilecompleted

Events triggered after an asynchronous upload file completes

Other Notes

Serial number

Description

1

Support Proxy Server.

You can explicitly assign a value to a proxy property, or you can pass a proxy object in a constructor. If the agent requires a login user and password, set the Credentials property of the proxy, for example:

Proxy.credentials = new NetworkCredential (UserName, Password)

Without an agent, you do not need to assign a value to the Proxy property or to pass the proxy parameter in the constructor.

2

The maximum size of the download file and upload file is limited by Int32.MaxValue , so the maximum processing can only be 2,147,483,647 bytes. If you want to work with larger files, modify the code in your code to open and read and write parts of the file to suit your requirements.

3

Progress percent trigger events and download (upload) Completion events are supported only when uploading files asynchronously or downloading files asynchronously.

4

Because the server does not send a file size, the passive FTP file transfer always displays a percentage of progress as zero. To display progress, you can change the FTP connection to an active state by overriding the GetWebRequest virtual method. So, we use the inner class in the Clsftp class Mywebclient

5

When listing files and directories, the Clsftp class distinguishes between the response data obtained and which are files and which are directories. At the same time, considering the FTP server above the directory style for MS DOS and UNIX two different forms of performance.

6

Supports the ability to move and copy files between different directories on the FTP server. The way to move a file is ingenious, but the method used to copy the file is not ideal.

Pack and download Discuz more useful
Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.