Asp.net is a fully encapsulated FTP class.

Source: Internet
Author: User
Tags ftp connection ftp file ftp login ftp file transfer

First look at this simple Copy codeThe Code is 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) * 100)/ftp. FileSize );
Response. Write (perc. ToString () + "% <br/> ");
Response. Flush ();
}

Function

Ü create an FTP Link

Ü support proxy

Ü multiload synchronous File Upload

Ü multiple overloaded asynchronous file uploads

Ü multiple overloaded files for Synchronous download

Ü multiple overloaded asynchronous file downloads

Ü progress percentage of uploaded files

Ü whitelist of object download progress

Ü differentiated list of directory or file information (considering the ms dos display format and UNIX format)

Ü directory or file existence judgment

Ü delete an object

Ü rename files and directories

Ü copy and move files on the FTP server (many may be searching for this feature)

Ü create and delete directories

Ü directory switching (very novel)

Common Constructor

Name

Description

ClsFTP ()

Initialize a new instance of the clsFtp class

ClsFTP (Uri FtpUri, string strUserName, string strPassword)

New clsFtp instances are initialized by the FTP address, FTP login user information, and FTP login user password.

ClsFTP (Uri FtpUri, string strUserName, string strPassword, WebProxy objProxy)

A new clsFtp instance is initialized by the FTP address, FTP login user information, FTP login user password, and Proxy Server Object.

Public attributes

Name

Description

Uri

Obtain or set the FTP server address

DirectoryPath

Get current working directory

UserName

Obtain or set an FTP login user

Password

Obtain or set the FTP logon Password

Proxy

Obtains or sets the proxy service used to connect to the FTP server.

ErrorMsg

Obtain the error message returned by the operation.

Common Methods

Name

Description

DownloadFile (string RemoteFileName, string LocalPath)

Download the file from the FTP server and save the file 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 and specify the local path and local file name.

Parameter description:

LocalPath: local path

LocalFilePath: local path for saving the file, followed "\"

LocalFileName: Save the local file name

DownloadFile (string RemoteFileName)

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

Parameter description:

RemoteFileName: Remote File Name

DownloadFileAsync (string RemoteFileName, string LocalPath, string LocalFileName)

Download files asynchronously from the FTP server and specify the local path and local file name.

Parameter description:

RemoteFileName: Remote File Name

LocalPath: local path for saving the file, followed "\"

LocalFileName: Save the local file name

DownloadFileAsync (string RemoteFileName, string LocalFullPath)

Download files asynchronously from the FTP server and specify 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: Indicates whether to 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: the file name to be saved 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: the file name to be saved on the FTP server

OverWriteRemoteFile: Indicates whether to 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: the file name to be saved on the FTP server

UploadFile (byte [] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)

Upload files to the FTP server.

Parameter description:

FileBytes: Binary File Content

RemoteFileName: the file name to be saved on the FTP server

OverWriteRemoteFile: Indicates whether to overwrite files with the same name on the remote server.

UploadFileAsync (string LocalFullPath)

Upload files to the FTP server asynchronously.

Parameter description:

LocalFullPath: local file name with full path

UploadFileAsync (string LocalFullPath, bool OverWriteRemoteFile)

Upload files to the FTP server asynchronously.

Parameter description:

LocalFullPath: local file with full path

OverWriteRemoteFile: Indicates whether to overwrite files with the same name on the remote server.

UploadFileAsync (string LocalFullPath, string RemoteFileName)

Upload files to the FTP server asynchronously.

Parameter description:

LocalFullPath: local file with full path

RemoteFileName: the file name to be saved on the FTP server

UploadFileAsync (string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)

Upload files to the FTP server asynchronously.

Parameter description:

LocalFullPath: local file name with full path

RemoteFileName: the file name to be saved on the FTP server

OverWriteRemoteFile: Indicates whether to overwrite files with the same name on the remote server.

UploadFileAsync (byte [] FileBytes, string RemoteFileName)

Upload files to the FTP server asynchronously.

Parameter description:

FileBytes: uploaded binary data

RemoteFileName: the file name to be saved on the FTP server

UploadFileAsync (byte [] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)

Upload files to the FTP server asynchronously.

Parameter description:

FileBytes: Binary File Content

RemoteFileName: the file name to be saved on the FTP server

OverWriteRemoteFile: Indicates whether to overwrite files with the same name on the remote server.

ListFilesAndDirectories ()

List all files and directories in the current directory on the FTP server.

ListFiles ()

List all files in the current directory on the FTP server.

ListDirectories ()

List all directories in the current directory on the FTP server.

DirectoryExist (string RemoteDirectoryName)

Determines whether the specified subdirectory in the current directory exists.

Parameter description:

RemoteDirectoryName: Specifies the directory name.

FileExist (string RemoteFileName)

Determine whether a remote file exists in the current directory of the server.

Parameter description:

RemoteFileName: Remote File Name

DeleteFile (string RemoteFileName)

Delete a file from the FTP server.

Parameter description:

RemoteFileName: Remote File Name

ReName (string RemoteFileName, string NewFileName)

Change the name of a file or directory.

Parameter description:

RemoteFileName: name of the original file or directory

NewFileName: name of the new file or directory

CopyFileToAnotherDirectory (string RemoteFile, string DirectoryName)

Copy a file under the current directory to another directory on the server. Note that after the file is copied, the current working directory is still the original directory of the file.

Parameter description:

RemoteFile: file name in the current directory

DirectoryName: name of the new directory.

Note: If the new directory is a subdirectory of the current directory, specify the subdirectory directly. Example: SubDirectory1/SubDirectory2;

If the new directory is not a subdirectory of the current directory, it must be specified at the root directory level. For example:./NewDirectory/SubDirectory1/SubDirectory2

MoveFileToAnotherDirectory (string RemoteFile, string DirectoryName)

Move a file under the current directory to another directory on the server. Note that after moving the file, the current working directory is still the original directory of the file.

Parameter description:

RemoteFile: file name in the current directory

DirectoryName: name of the new directory.

Note: If the new directory is a subdirectory of the current directory, specify the subdirectory directly. Example: SubDirectory1/SubDirectory2;

If the new directory is not a subdirectory of the current directory, it must be specified at the root directory level. For example:./NewDirectory/SubDirectory1/SubDirectory2

MakeDirectory (string DirectoryName)

Create a sub-directory in the current working directory on the FTP server.

Parameter description:

DirectoryName: subdirectory name

RemoveDirectory (string DirectoryName)

Delete a sub-directory from the current working directory.

Parameter description:

DirectoryName: subdirectory name

IsValidPathChars (string DirectoryName)

Determines whether the characters in the directory name are valid.

Parameter description:

DirectoryName: Directory Name

IsValidFileChars (string FileName)

Determines whether the characters in the file name are valid.

Parameter description:

FileName: File Name

GotoDirectory (string DirectoryName)

Enter a directory.

Parameter description:

DirectoryName:

The name of the new directory.

Note: If the new directory is a subdirectory of the current directory, specify the subdirectory directly. Example: SubDirectory1/SubDirectory2;

If the new directory is not a subdirectory of the current directory, it must be specified at the root directory level. For example:./NewDirectory/SubDirectory1/SubDirectory2

EnterOneSubDirectory (string DirectoryName)

Enter a sub-directory from the current working directory.

Parameter description:

DirectoryName: subdirectory name

ComeoutDirectory ()

From the current working directory to the upper-level directory.

Public Events

Name

Description

DownloadProgressChanged

Events triggered when the asynchronous download progress changes

DownloadDataCompleted

Events triggered after the asynchronous file download is complete

UploadProgressChanged

Events triggered when the asynchronous upload progress changes

UploadFileCompleted

Events triggered after the asynchronous file upload is complete

Other Instructions

Serial number

Description

1

Supports proxy servers.

You can assign values to the Proxy attribute explicitly, or pass the Proxy object in the constructor. If the Proxy needs to log on to the user and password, set the Credentials attribute of the Proxy, for example:

Proxy. Credentials = new NetworkCredential (UserName, Password)

If no Proxy exists, you do not need to assign values to the Proxy attribute or pass the Proxy parameter in the constructor.

2

Maximum size of downloaded and uploaded filesInt32.MaxValueLimit, so the maximum processing capacity is 2,147,483,647 bytes. If you want to process a larger file, modify the code for opening and reading/writing the file in the code to meet your requirements.

3

The progress percentage trigger and download (upload) completion events are supported only when files are uploaded asynchronously or asynchronously.

4

Because the server does not send the file size, Passive FTP File Transfer always shows the progress percentage to zero. To display the progress, you can rewrite the GetWebRequest virtual method to change the FTP connection to the active status. Therefore, we also use the internal class MyWebClient in the clsFTP class.

5

When listing files and directories, The clsFTP class separates the obtained response data from files and directories. At the same time, the directory style on the FTP server is ms dos and UNIX.

6

Supports moving and copying files between different directories on the FTP server. The method for moving files is clever, but the method for copying files is not ideal.

It is easier to package and download discuz

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.