Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient.DownloadFileAsync Method : How to retrieve the Object?

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

like image 351
panindra Avatar asked Feb 20 '23 04:02

panindra


1 Answers

The object you sent should be accessible via e.UserState property

try this

var file = (FileLibrary) e.UserState

like image 172
Haris Hasan Avatar answered Mar 02 '23 23:03

Haris Hasan