0

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?

4

1 回答 1

0

You can try by creating the service request.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(SessionConstants.serviceURL, UriKind.Absolute));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(ObjectListRequestReady), request);

Then Send the service request to the server for upload the file.

void ObjectListRequestReady(IAsyncResult asyncResult)
{

     HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
     Stream stream = request.EndGetRequestStream(asyncResult);

     Deployment.Current.Dispatcher.BeginInvoke(delegate()
     {
            SELECTED_NODE = SPSITETextBox.Text;
            StreamWriter writer = new StreamWriter(stream);
            writer.WriteLine("pass your data to upload on server");
            writer.Flush();
            writer.Close();
            request.BeginGetResponse(new AsyncCallback(ObjectListResponseReady), request);

            // ObjectListResponseReady is the callback method called after uploading the data.
      });
}

Get the Result from the service. ObjectListResponseReady is the callback method which ack you after uploading the file.

void ObjectListResponseReady(IAsyncResult asyncResult)
{
        try
        {
            HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                Stream responseStream = response.GetResponseStream();
                XmlReader xmlReader = XmlReader.Create(responseStream);
            });
            }

        catch (Exception ex)
        {
            Message.ErrorMessage("error: " + ex);
        }
}
于 2012-06-20T18:45:25.730 回答