简体   繁体   English

使用WebCLient发送文件:什么是userToken?

[英]Sending file using WebCLient: What is userToken?

I've been trying to upload file to server over WebClient in a SileverLight app. 我一直在尝试通过SileverLight应用程序中的WebClient将文件上传到服务器。 I've come across this OpenWriteAsync method and the first two arguments it gets are clear but the third is of type object and it's name is UserToken so although I made a lot of search I couldn't find a clear description on what it is and how it should be used. 我遇到过这个OpenWriteAsync方法,它得到的前两个参数很明确,但是第三个参数是object类型,它的名称是UserToken所以尽管我进行了很多搜索,但我找不到关于它的明确描述,并且应该如何使用。 (UserToken parameter). (UserToken参数)。 So what goes in there? 那里面有什么?

A user-defined object that is.... that Microsoft has provided is an awful explanation. Microsoft提供的用户定义的对象是一个可怕的解释。 I mean they had to provide a map of all the properties that can go in there like : 我的意思是他们必须提供所有可以进入的属性的地图,例如:

new {param1 = value, param2= value}

Theres no way a developer can guess how the user-defined object works naturally. 开发人员无法猜测用户定义的对象是如何自然工作的。

Basically it's whatever you want it to be. 基本上,这就是您想要的。 It contains state data that will be passed to the event args in the OpenWriteCompleted event. 它包含将在OpenWriteCompleted事件中传递给事件args的状态数据。 The WebClient doesn't use this data in any way other than passing it on. WebClient除了传递数据外,不以其他任何方式使用此数据。

private void OpenWrite()
{
    webClient.OpenWriteCompleted += webClient_OpenWriteCompleted;

    // I'm just using this as an example. It can be any data type, but I am using byte[] so I can write it to the stream later.
    byte[] data = new byte[] { 0, 1, 3, 4 }; 

    webClient.OpenWriteAsync(uri, method, data);
}

private void webClient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
    // Now e.UserState contains whatever data you passed as the userToken.
    byte[] data = (byte[])e.UserState;

    // Now write this data to the stream
    e.Result.Write(data, 0, data.Length);
    e.Result.Close();
}

If you don't need to pass any state information, just pass null . 如果您不需要传递任何状态信息,只需传递null

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

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