I am developing a Silverlight custom control where I am sending some XML data to my server using WebClient. After the file is uploaded I need to run a callback function. So, the question is how do I know it is done uploading the file? Here is the code I wrote:
WebClient wc = new WebClient();
wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
Uri uri = new Uri(myUrl);
wc.OpenWriteAsync(uri, "POST", data);
...
private void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e){
if (e.Error == null)
{
byte[] fileContent = e.UserState as byte[];
Stream outputStream = e.Result;
outputStream.Write(fileContent, 0, fileContent.Length);
outputStream.Flush();
outputStream.Close();
}
HtmlPage.Window.Invoke("callBackFunction");
}
That all looked good to me but there are two problems. One I am not sure that wc_OpenWriteCompleted is actually running when the file is done uploading. How do I confirm that? The call back function, if the file is not complete it is not going to do anything. Is that the right place to put that call back? Last, the server responds with some test after the call is placed, how can I pass that to my callback function?