views:

200

answers:

2

I follow the example in to upload file to server successfully http://www.c-sharpcorner.com/UploadFile/nipuntomar/FileUploadsilverlight03182009030537AM/FileUploadsilverlight.aspx

is it possible to get a string message from server on the webclient OpenWriteCompleted event?

A: 

On your HttpHandler you'll just need

context.Response.Write("You made it");

To read it on Silverlight side, you'll probably need to deal with OpenReadCompleted event.

Rubens Farias
OpenReadCompleted only fires when OpenReadAsync is used.
AnthonyWJones
+3  A: 

Unfortunately this is one of a number of bizare design choices for the Silverlight WebClient, you can't access the Response easily having performed a POST. Its really odd since most POST operations have a useful response body.

However there are a number of things you can do. You could ditch the WebClient and use WebRequest/WebResponse directly. You could inherit off the WebClient and override GetWebResponse so you can intercept it.

However a sneaky option if your string message is reasonably short is to add a custom HTTP header to the response.

The thread executing OpenWriteCompleted will block when the output stream is closed until a response is received. At that point you can access the ResponseHeaders collection on the WebClient object to retrieve the value of your custom header. (Why the Response stream hasn't been made available at the point escapes me!)

AnthonyWJones
Can you introduce more detail that how to add a "custom HTTP header" in the ashx file? Thanks~
Jay