打开响应流在Silverlight [英] Opening response stream in silverlight

查看:257
本文介绍了打开响应流在Silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从使用Silverlight 3的服务器的服务器返回一个图像恢复这样的响应流:

I am attempting to return a image from a server using Silverlight 3. The server returns the Response stream like this:

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

在Silverlight客户端,我喜欢处理数据流:

On the Silverlight client I am handling the stream like:

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

我要带该流并打开浏览器保存对话框,可以选择我已经探讨是使用Html.Window.Navigate(新的URI(图像URL)),这打开了正确的浏览器默认对话框,但它是不是一种选择,因为我需要通过发送扩展的信息(例如,XML)到服务器的Htt prequest.Headers.Item和导航不允许这种

I want to take that stream and open the browsers save dialog, one option I have explored is using the Html.Window.Navigate(New Uri("image url")) and this opened the correct browser default dialog but it is not an option because I need to send extended information(e.g. XML) to the server through the HttpRequest.Headers.Item and the Navigate doesn't allow this.

我如何可以采取一个响应流,并强制默认浏览器保存对话框从Silverlight应用程序出现不使用Html.Window.Navigate(新的URI(图像URL))?

How can I take a Response Stream and force the default browser Save dialog to appear from the Silverlight Application without using the Html.Window.Navigate(New Uri("image url"))?

推荐答案

直答案是不能,Silverlight的SaveFileDialog只能开一个用户交互的直接结果,如按一下按钮。

The straight answer is you can't, the Silverlight SaveFileDialog can only be opened as a direct result of a user interaction such as a button click.

到这类问题(您想上传)的解决方案是将XML发布到存储说,在会话对象或文件服务器。响应是一些处理,你可以用它来检索XML,如GUID。

The solution to this sort of issue (where you want an upload) is to Post the XML to the server for storage say in the session object or as file. The response is some handle you can use to retrieve the XML such as GUID.

您就可以使用标准的导航放置GUID在URL的查询字符串。接收脚本(ashx的是这种情况),可以检索previously发布使用XML在URL中提供的句柄。

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

您还需要code这样的服务器端的响应: -

You'll also want to code the server-side response like this:-

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

这会导致浏览器显示打开或保存对话框。通常情况下,当前窗口的导航状态保持,所以你SL应用程序应保持其目前的状态,但我还没有真正测试。

this will cause the browser to display an "Open or Save dialog". Normally the navigation state of the current window is maintained so you SL app should remain in its current state but I've not actually test that.

顺便说一句,注意没有到Response.End(),这就是做一个可怕的事情,如果你能避免它再这样做。

BTW, note no Response.End(), thats an awful thing to do, if you can avoid it then do so.

这篇关于打开响应流在Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆