ESFramework Demo -- file transfer Demo (with source code)

Source: Internet
Author: User

Now we will use the third weapon provided by ESPlus to add the file transfer function on the basis of ESFramework Demo-Getting Started Demo and a simple instant messaging system. 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.

The demo in this article only implements file transfer between the client and the client. As for the transfer folder and the file transfer between the server and the client, the model is exactly the same, you can expand it on the basis of this demo.

This Demo demonstrates the following features related to file transfer:

(1) When the sender requests to send a file, the receiver can agree or reject the receiving of the file.

(2) During file transfer, either party can learn the real-time progress of file transfer through the event.

(3) During file transfer, either party sending or receiving the file can interrupt the transfer of the file.

(4) When either party is disconnected during file transfer, the file transfer will be interrupted.

(5) As long as the file transfer is interrupted, the sender and receiver will receive corresponding Event Notifications.

(6) Enable resumable upload.

(7) after the file is transferred, the sender and receiver will receive corresponding Event Notifications.

I. Server

In the demo, the server does not participate in file transfer, so the server code does not need to be modified. You can directly use the server in the previous demo.

By the way, if you want the server to be the recipient of the file, it is also easy to follow the following points:

(1) Use the FileController attribute exposed by IRapidServerEngine to Control File Sending and receiving behaviors.

(2) reserve the FileSendingEvents event and FileReceivingEvents event of IFileController to track the real-time status of file transfer.

(3) the virtual account of the server is NetServer. SystemUserID, that is, "_ 0 ". When the UserID of the recipient of a file is NetServer. systemUserID, indicating that the file is received by the server; when the UserID of the sender of a file is NetServer. systemUserID, indicating that the file is sent by the server.

Ii. Client

Compared with the previous demo, the client changes mainly on MainForm and ChatForm. The client uses the FileOutter attribute exposed by IRapidPassiveEngine to Control File Sending and receiving behaviors.

Sending and receiving process implementation

First, a "Send File" button is added at the top of ChatForm. When you click this button, the file is selected and the request is sent. In this case, the client appears as the sender.

// Send a file request
Private void toolstripbutton#click (object sender, EventArgs e)
{
String filePath = ESBasic. Helpers. FileHelper. GetFileToOpen ("open ");
If (filePath = null)
{
Return;
}
String fileID;
SendingFileParas sendingFileParas = new SendingFileParas (20480, 0); // file data packet size, which can be set according to network conditions. The local network can be set to 204800, and the transmission speed can reach more than 30 Mb/s; recommended public network value: 2048, 4096, or 8192
This. fileOutter. BeginSendFile (this. friendID, filePath, null, sendingFileParas, out fileID );
}

In the Initialize method of MainForm, the client subscribes to the FileRequestReceived event of FileOutter of IRapidPassiveEngine to receive the notification of receiving the file transfer request. In this case, the client is the identity of the receiver.

// Scheduled to receive an event from the sender sending a file (CLIP) Request
This. rapidPassiveEngine. FileOutter. FileRequestReceived + = new CbFileRequestReceived (fileOutter_FileRequestReceived );

Note that the FileRequestReceived event has a ResumedProjectItem parameter to indicate whether the current transfer project can be resumed. If this parameter is not null, it indicates that data can be resumed. If this parameter is null, it indicates a completely new transfer project and cannot be resumed.

In the event processing function, MessageBox is displayed, asking whether the user agrees to receive the event. If yes, select the Save path. According to the user's operations, we need to notify the sender of whether to agree to receive the answer to the file. That is, the OnFileRequest method of ChatForm:

// When receiving a file transfer request from the other party
Public void OnFileRequest (string fileID, string senderID, string fileName)
{
If (DialogResult. OK = MessageBox. Show (string. Format ("{0} requires you to transfer the file {1}, do you agree to receive it? ", SenderID, fileName)," file transfer ", MessageBoxButtons. OKCancel ))
{
String savePath = ESBasic. Helpers. FileHelper. GetPathToSave ("save", fileName, null );
If (! String. IsNullOrEmpty (savePath ))
{
This. fileOutter. BeginReceiveFile (fileID, savePath );
}
Else
{
This. RejectFile (fileID, fileName );
}
}
Else
{
This. RejectFile (fileID, fileName );
}
}
  private void RejectFile(string fileID, string fileName)
  {
      TransferingProject fileInfo = this.fileOutter.GetTransferingProject(fileID);
      if (fileInfo != null)
      {
          this.textChatControl1.ShowRejectFile(fileName);
          this.fileOutter.RejectFile(fileID);
       }
  }

In the Initialize method of MainForm, the sender schedules the FileResponseReceived event of FileOutter of IRapidPassiveEngine to get the reply from the other party:

// The recipient replies to the event that the recipient agrees to or rejects receiving the file (folder ).
This. rapidPassiveEngine. FileOutter. FileResponseReceived + = new CbGeneric <TransferingProject, bool> (fileOutter_FileResponseReceived );

If the recipient agrees to receive the message, ESPlus will automatically start the File Sending thread in the background to send the file.

Both the client as the sender and receiver use the FileTransferingViewer control in ChatForm to display the real-time status of each file transfer project, and receive notifications by booking its status change events, then, the corresponding information is displayed in the chat window.

 

After the Demo runs, the file is transferred as follows:

 

When the ChatForm function is closed in the chat window, check whether any file is being transferred. If yes, the user is reminded that, if the user still decides to close the form, cancel the relevant transfer-in project before closing the form.

Private void ChatForm_FormClosing (object sender, FormClosingEventArgs e)
{
List <string> fileIDs = this. fileOutter. GetTransferingAbout (this. friendID );
If (fileIDs! = Null & fileIDs. Count> 0)
{
DialogResult result = MessageBox. Show ("if the window is closed, file transfer is aborted. Close the window? "," Prompt ", MessageBoxButtons. OKCancel, MessageBoxIcon. None );
If (result = DialogResult. OK)
{
This. fileOutter. CancelTransferingAbout (this. friendID );
}
Else
{
E. Cancel = true;
}
}

}
Resumable upload

When the file transfer is interrupted (for example, because of a disconnection), the sender sends the file again. If the recipient agrees to receive the file and the Save path is exactly the same as the previous one, the underlying framework automatically enables resumable data transfer. The sender and receiver can learn the information about the start of resumable Upload by booking the FileResumedTransStarted event of FileTransferingViewer.

More simply, as mentioned above, the ResumedProjectItem type parameter of the FileRequestReceived event. If its value is not null, it indicates that it can be resumed. If you select resume, you can directly use the value of the LocalSavePath attribute of ResumedProjectItem as the file storage path and call the BeginReceiveFile method of IFileOutter. If you are interested, you can modify the demo code in this mode to achieve the resume effect similar to QQ.

3. FileTransferingViewer Control

The FileTransferingViewer control is used to display the real-time status of each file transfer project. Compared with the QQ File Transfer UI, FileTransferingViewer is just a simple control for demo. Here we open the source code of FileTransferingViewer to everyone. You can modify the source code to achieve the desired effect (or the same effect as QQ ).

Many other details have been detailed in the ESFramework development manual (03)-file (CLIP) Transfer article, and will not be repeated here. You can refer to the above text and the article, and then compare the source code for research, it is easy to understand the internal operation process.

4. Download source code

(1) ESFramework. Demos. FileTransfer source code

(2) source code of the FileTransferingViewer Control

 

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.