简体   繁体   English

WebClient.DownloadFileAsync方法:如何检索对象?

[英]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 . 我试图从服务器异步下载文件,我正在使用weblcient为此目的。 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. 在Asyschromously下载文件时,我在DownloadFileAsync(Uri地址,字符串fileName,Object)方法用户令牌中发送文件(类文件库的isntance),但是我无法从调用的方法中检索我发送的对象下载完成后。 here is the code details 这是代码细节

public void DownloadFileAsync( Uri address, string fileName, Object userToken ) public void DownloadFileAsync(Uri地址,字符串fileName,Object userToken)

and MSDN says > userToken 和MSDN说> userToken

Type: System.Object A user-defined object that is passed to the method invoked when the asynchronous operation completes. 类型:System.Object传递给异步操作完成时调用的方法的用户定义对象。

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'. 错误是: 无法将类型为“System.Net.WebClient”的对象强制转换为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. Accorrding我上面描述的理解,对象应该被强制转换为易于filelibrary becouse我在同类型的呼叫发送的文件名。 why i am getting error or is there any other method to retrieve the sent Object:userToken 为什么我收到错误或有任何其他方法来检索发送的对象:userToken

The object you sent should be accessible via e.UserState property 您发送的对象应该可以通过e.UserState属性访问

try this 试试这个

var file = (FileLibrary) e.UserState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM