简体   繁体   中英

Sending file using WebCLient: What is userToken?

I've been trying to upload file to server over WebClient in a SileverLight app. 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. (UserToken parameter). So what goes in there?

A user-defined object that is.... that Microsoft has provided is an awful explanation. 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. The WebClient doesn't use this data in any way other than passing it on.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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