Opening a response flow in Silverlight

I am trying to return an image from a server using Silverlight 3. The server returns a Response stream as follows:

context.Response.ContentType = imageFactory.ContentType imgStream.WriteTo(context.Response.OutputStream) imgStream.Close() context.Response.End() 

On the Silverlight client, I treat the stream as:

  Dim request As HttpWebRequest = result.AsyncState Dim response As HttpWebResponse = request.EndGetResponse(result) Dim responseStream As IO.Stream = response.GetResponseStream() 

I want to take this stream and open the browser save dialog, one of the options I studied uses Html.Window.Navigate (New Uri ("image url")), and this opened the correct browser dialog, but it’s not an option, because that I need to send extended information (for example, XML) to the server through HttpRequest.Headers.Item, and Navigate does not allow this.

How can I take a response stream and force the default browser browser dialog to be displayed from a Silverlight application without using Html.Window.Navigate (New Uri ("image url")?

+3
source share
1 answer

The direct answer: you cannot, Silverlight SaveFileDialog can be opened only as a direct result of user interaction, for example, pressing a button.

The solution to this problem (where you want to download) is to send XML to the server for storage, say, in a session object or as a file. The answer is some kind of handle that you can use to retrieve XML, such as a GUID.

You can then use standard navigation by placing the GUID in the query string of the URL. The receiving script (ashx in this case) can retrieve the previously hosted XML using the handle specified in the URL.

You will also want to encode the server-side response as follows: -

 context.Response.ContentType = imageFactory.ContentType; context.Response.AddHeader("Content-Disposition", "attachment;file=someimage.jpg"); imgStream.WriteTo(context.Response.OutputStream); imgStream.Close(); 

this will cause the browser to display the "Open or Save" dialog box. Normally, the navigation state of the current window is maintained so that the SL application remains in its current state, but I have not actually tested it.

By the way, don’t notice Response.End (), this is a terrible thing, if you can avoid it, do it.

+2
source

Source: https://habr.com/ru/post/1369015/


All Articles