ESFramework Demo-Simple Network hard disk Demo (with source code)

Source: Internet
Author: User

The core function of the FTP server is to provide file upload and download services. In ESFramework Demo-File Transfer Demo (with source code), we demonstrated how to transfer files between the client and the client. Now we can implement a simple FTP server, to demonstrate how to transfer files between the client and the server. Before reading this article, you must first master the file transfer process and related API usage described in ESFramework development manual (03)-file (folder) transfer.

This Demo mainly demonstrates the following functions:

(1) The client browses all files in the default directory of the server.

(2) The client uploads files to the default directory of the server.

(3) The client can download any file in the default directory of the server.

1. Define information types

According to the functional requirements mentioned above, we need to define the corresponding information types:

Public static class FtpInformationTypes {/// <summary> /// obtain all file names. C-> S // </summary> public const int GetAllFileNames = 0; // <summary> // request to download the object. C-> S // </summary> public const int DownloadFile = 1 ;}

You do not need to define additional information types when uploading files. You can directly use IFileOutter's request to send files.

Ii. Server

The server sets the file directory to the "FileFold" folder under the running directory. All uploaded files will be saved to this directory, and all files to be downloaded will also come from this directory.

The CustomizeHandler class of the server implements the custom Information Processor interface ICustomizeHandler. When receiving the FtpInformationTypes. GetAllFileNames synchronous call from the client, it returns the list of all files in the FileFold directory to the client. When receiving the request to download the file, call the IFileController. BeginSendFile method to send the specified file to the client.

When the client uploads a file, it will directly call the BeginSendFile of IFileOutter. At this time, the server will trigger the IFileController FileRequestReceived event. Therefore, the server needs to schedule and handle this event:

    void fileController_FileRequestReceived(string fileID, string senderID, string fileName, ulong fileLength, ESPlus.FileTransceiver.ResumedProjectItem resumedProjectItem, string comment)    {            int index = fileName.LastIndexOf('\\');            string filePath = string.Format(@"{0}FileFold\{1}", AppDomain.CurrentDomain.BaseDirectory, fileName.Substring(index +1));            this.fileController.BeginReceiveFile(fileID, filePath);    }

The server sets the path for saving the file to the FileFold directory, and then calls the IFileController. BeginReceiveFile method to start receiving the file. Of course, the processing here is much simplified, such as not determining whether the disk space is sufficient or whether there are files with the same name.

Iii. Client

After successfully logging on to the client, go to the main interface. During initialization of the main interface, FtpInformationTypes. GetAllFileNames will be sent to the server for Synchronous calling, and the returned file list will be displayed in ListView.

Double-click a file in ListView and send the FtpInformationTypes. DownloadFile information to the server. As described above, the server will call IFileController. the BeginSendFile method sends the specified file, and the client also triggers IFileOutter. fileRequestReceived event. When processing this event, let us select the path to be stored.

Void fileOutter_FileRequestReceived (string projectID, string senderID, string fileName, ulong totalSize, ResumedProjectItem resumedFileItem, string comment) {if (this. invokeRequired) {this. invoke (new CbReadyToAcceptFileAsyn (this. fileOutter_FileRequestReceived), projectID, senderID, fileName, totalSize, resumedFileItem, comment);} else {string savePath = ESBasic. helpers. fileHelper. getPathToSave ("save", fileName, null); if (string. isNullOrEmpty (savePath) {this. fileOutter. rejectFile (projectID);} else {this. fileOutter. beginReceiveFile (projectID, savePath );}}}

If you cancel the Save path selection, the downloaded file is discarded. In this case, IFileOutter. RejectFile is called to cancel the operation.
When the client clicks the upload button, it directly calls IFileOutter. BeginSendFile to prepare for file upload.

Private void toolStripButton_upLoad_Click (object sender, EventArgs e) {string filePath = ESBasic. helpers. fileHelper. getFileToOpen ("open"); if (filePath = null) {return;} string fileID; SendingFileParas sendingFileParas = new SendingFileParas (2048, 0); // file packet size, it can be set according to the network conditions, the local network can be set to 204800, the transmission speed can reach 30 Mbit/s or more, and the public network is recommended to set to 2048 or 4096 or 8192 this. fileOutter. beginSendFile (NetServer. systemUserID, filePath, null, sendingFileParas, out fileID );}

This will trigger the FileRequestReceived event of the IFileController on the server, and then the server will call the IFileController. BeginReceiveFile method to start the formal transfer of the file.

When the client is uploading and downloading files:

This is the simplest demo to demonstrate the file upload/download function. It is very rough and only used to demonstrate how to use the file transfer function provided by ESPlus to transfer files between the server and the client. To officially develop a file server system, this article can only be a simple starting point, and there are still many complicated things to do, which is beyond the content of this article, but if you have any idea, welcome to discuss with us.

4. Download source code

ESFramework. Demos. Ftp source code

 

Read more ESFramework development manual articles.

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

For any questions about ESFramework, please contact us:

Tel: 027-87638960

Q: 372841921

Mail: esframework@oraycn.com

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.