How to user BeginUpload?

Hi,

Can you give me an example or point me to a link that shows how to use BeginUpload method to upload the files using Aspose.FTP.

Thanks,

Jerry

Hi Jerry,

Below is an example of using BeginUpload() method. This is an asynchronous method that takes a delegate of type System.AsyncCallback. This delegate takes a callback function as an argument. This callback function will be called automatically when the file upload will finish.

///


/// upload a file using BeginUpload method
///

public void UploadFile()
{
try
{
Console.WriteLine("Connecting to FTP server..... ");
FtpClient ftpClient = new FtpClient("127.0.0.1", 21, "anonymous", "password");
ftpClient.Connect();
ftpClient.Login();
Console.WriteLine("Connection successfull.");
object objState = new object(); // state object

// this is the delegate that registers callback function
AsyncCallback callBack = new AsyncCallback(uploadComplete);

Console.WriteLine("Call BeginUpload().");
ftpClient.BeginUpload(@"c:\Test Upload.txt", @"\Test Upload.txt", objState, uploadComplete);
// the above function executes asynchronously.
// Next statement will be executed without waiting for the function to complete
Console.WriteLine("After BeginUpload() call.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

///


/// callback function that is called automatically
/// when asynchronous function BeginUpload() finishes
///

///
private void uploadComplete(IAsyncResult result)
{
// this message will be displayed when the file upload completes
Console.WriteLine("File upload complete.");
}

Asynchronous functions are useful when the function takes long time to execute and you do not want to wait for it to be finished.

Have a nice time.

Regards,
Saqib Razzaq

Hi Razzaq,

Thanks very much for posting the code! I really appreciate it.

I have another question about BeginUpload() function though, how can I cancel the upload while it is in progress?

Hi Jerry,

You can use BeginCloseControlChannel() function to cancel the upload process. Please add the following line in your code.

ftpClient.BeginCloseControlChannel(objState, callBack);

You can pass the state object and the callback function to the BeginCloseControlChannel() method to cancel the upload.

Regards,
Saqib Razzaq

Thanks for your considering Aspose.

1. You can close the control channel and data channel by calling Abort(void) function.

2. You can close data channel by calling CloseDataChannel(void) and close the control channel by calling CloseControlChannel(void) seperately.

3. They all have the asynchrousing versions.

Best regards

Thanks,