I am trying to downaload file asynchronously from the server and i am useing weblcient for this purpose . While downloading the file Asyschromously , i am sending file( isntance of class file library) in the DownloadFileAsync( Uri address, string fileName, Object) method user-token ) but i am failing to retrieve the object i sent in this from method whihc invoked after download completed. here is the code details
public void DownloadFileAsync( Uri address, string fileName, Object userToken )
and MSDN says > userToken
Type: System.Object A user-defined object that is passed to the method invoked when the asynchronous operation completes.
actually , i am passing the file name in the calling :
wc.DownloadFileAsync(uri, file.StorePath,file);
and my download complete method is
void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
var file = (FileLibrary)sender;
this.DownloadedFileName = file.FileId;
}
Error is : Unable to cast object of type 'System.Net.WebClient' to type FileLibrary'.
Accorrding to my understanding of above description, the object should be cast to filelibrary easily becouse the file name i sent in the calling of the same type. why i am getting error or is there any other method to retrieve the sent Object:userToken
The object you sent should be accessible via e.UserState property
try this
var file = (FileLibrary) e.UserState
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With