The following walkthrough demonstrates the steps required to setup file-transfers; It demonstrates how to send as well as how to receive files using the built-in file-transfer methods of XMPP.

Transferring files

  1. In order to proccess incoming file-requests you must provide a delegate of type FileTransferRequest to the XmppClient instance. This delegate is invoked whenever another XMPP user attempts to initiate a file-transfer. It is then this method's responsibility to examine or present the provided meta-data (file name, file size and description) to the user in order to determine whether to accept or refuse the pending file-transfer request. The following example illustrates how to setup the FileTransferRequest delegate.

    CopyC#
    using S22.Xmpp.Client;
    using S22.Xmpp.Extensions;
    using System;
    using System.Threading;
    
    namespace ConsoleApplication54 {
        class Program {
            static void Main(string[] args) {
                string hostname = "jabber.se";
                string username = "myUsername";
                string password = "myPassword";
    
                using (XmppClient client = new XmppClient(hostname, username, password)) {
                    // Setup any event handlers here.
                    // ...
                    // Provide a FileTransferRequest delegate to the XmppClient instance.
                    client.FileTransferRequest = OnFileTransferRequest;
                    client.Connect();
    
                    Console.WriteLine("Connected as " + client.Jid);
                    Console.WriteLine("Waiting for incoming file-requests...");
                    // Block indefinitely.
                    Thread.Sleep(Timeout.Infinite);
                }
            }
    
            /// <summary>
            /// A callback method invoked whenever another user wishes to send us a file.
            /// </summary>
            /// <param name="transfer">
            /// A FileTransfer object containing meta-data about the
            /// respective file.
            /// </param>
            /// <returns>
            /// The location at which to save the incoming file, or null to refuse
            /// the file-request.
            /// </returns>
            static string OnFileTransferRequest(FileTransfer transfer) {
                // Let the user decide whether we want to accept the file-request or not.
                Console.WriteLine("Incoming file-transfer request from <" + transfer.From + ">: ");
                Console.WriteLine(" - Filename: " + transfer.Name);
                Console.WriteLine(" - Filesize: " + transfer.Size + " bytes");
                if (!String.IsNullOrEmpty(transfer.Description))
                    Console.WriteLine(" - Description: " + transfer.Description);
    
                Console.Write("Type Y to accept or N to refuse the request: ");
                string s = Console.ReadLine().ToLowerInvariant();
                if (s == "y")
                    // This saves the file in the current working directory of the process.
                    return transfer.Name;
                else
                    // Returning null indicates we don't wish to accept the file-transfer.
                    return null;
            }
        }
    }

    Upon accepting a pending request for a file, the progress of the file-transfer operation can be observed by subscribing to the FileTransferProgress event, which is raised periodically as the file is being transferred. If a file-transfer is aborted prematurely, the FileTransferAborted event is raised.

  2. In order to offer a file to another XMPP user, call the InitiateFileTransfer method; The method expects the JID of the recipient, the file you wish to transfer and optionally a callback method that is invoked once the other site has either accepted or rejected the file-transfer request. The following example demonstrates how to initiate a file-transfer with another XMPP user and print the progress of the subsequent file-transfer operation to the console.

    CopyC#
    using S22.Xmpp.Client;
    using S22.Xmpp.Extensions;
    using System;
    
    namespace ConsoleApplication1 {
        class Program {
            static void Main(string[] args) {
                string hostname = "jabber.se";
                string username = "myUsername";
                string password = "myPassword";
                string recipient = "king_olaf@jabber.no/laptop";
    
                using (XmppClient client = new XmppClient(hostname, username, password)) {
                    // Setup event handlers.
                    client.FileTransferProgress += OnFileTransferProgress;
                    client.FileTransferAborted += OnFileTransferAborted;
    
                    client.Connect();
                    Console.WriteLine("Connected as " + client.Jid);
    
                    while (true) {
                        Console.Write("Type 'send' to initiate the file-transfer, or 'quit' to exit: ");
                        string s = Console.ReadLine().ToLowerInvariant();
                        if (s == "quit")
                            return;
                        if (s == "send") {
                            client.InitiateFileTransfer(recipient, "polarbear_greenland.jpg",
                                "This is a picture of a polar bear from Greenland!", FileTransferCallback);
                        }
                    }
                }
            }
    
            /// <summary>
            /// The callback method invoked once the pending file-transfer has been accepted or
            /// rejected.
            /// </summary>
            /// <param name="accepted">
            /// true if the other site accepted our file-transfer request; Otherwise false.
            /// </param>
            /// <param name="transfer">
            /// A FileTransfer object containing meta-data about the file-transfer.
            /// </param>
            static void FileTransferCallback(bool accepted, FileTransfer transfer) {
                Console.WriteLine(transfer.To + " has " + (accepted == true ? "accepted " : "rejected ") +
                    "the transfer of " + transfer.Name + ".");
            }
    
            static void OnFileTransferProgress(object sender, FileTransferProgressEventArgs e) {
                // Print out the progress of the file-transfer operation.
                Console.WriteLine("Transferring " + e.Transfer.Name + "..." +
                    e.Transfer.Transferred + "/" + e.Transfer.Size + " Bytes");
            }
    
            static void OnFileTransferAborted(object sender, FileTransferAbortedEventArgs e) {
                Console.WriteLine("The transfer of " + e.Transfer.Name + " has been aborted.");
            }
        }
    }

See Also