2010-07-30 51 views
0

节省Silverlight的一个文件时,我有一个ASP.NET站点承载的Silverlight应用程序,通过它我开始一个HttpWebRequest的一个通用的处理程序,以节省CSV文件到用户的机器。PlatformNotSupportedException通过通用处理器

从Silverlight应用程序中,Uri是使用参数构建的,以便使CSV文件成为服务器端。单击一个按钮时,其触发以下:

string httpHandlerName = "HttpDownloadHandler.ashx"; 
// CustomUri handles making it an absolute Uri wherever we move the handler. 
string uploadUrl = new CustomUri(httpHandlerName).ToString(); 

UriBuilder httpHandlerUrlBuilder = new UriBuilder(uploadUrl); 
httpHandlerUrlBuilder.Query = string.Format("{3}startdate={0}&enddate={1}&partnerId={2}", startDate, endDate, partnerId, string.IsNullOrEmpty(httpHandlerUrlBuilder.Query) ? "" : httpHandlerUrlBuilder.Query.Remove(0, 1) + "&"); 

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(httpHandlerUrlBuilder.Uri); 
webRequest.Method = "POST"; 
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); 

现在,这里是从HttpDownloadHandler.ashx

public void ProcessRequest(HttpContext context) 
{ 
    _httpContext = context; 

    string partnerId = _httpContext.Request.QueryString["partnerId"]; 
    string startDate = _httpContext.Request.QueryString["startDate"]; 
    string endDate = _httpContext.Request.QueryString["endDate"]; 

    ExportCsvReport exportCsv = new ExportCsvReport(); 
    _csvReport = exportCsv.ExportMemberRegistrationReport(partnerId, startDate, endDate); 

    context.Response.Clear(); 
    context.Response.AddHeader("content-disposition", "attachment; filename=Report.csv"); 
    context.Response.ContentType = "text/csv"; 
    context.Response.Write(_csvReport); 
} 

这里的ProcessRequest代码是回来当保存文件对话拒绝获取HttpResponse头信息出现:

{System.Web.HttpResponse} 
Buffer: true 
BufferOutput: true 
Cache: {System.Web.HttpCachePolicy} 
CacheControl: "private" 
Charset: "utf-8" 
ContentEncoding: {System.Text.UTF8Encoding} 
ContentType: "text/csv" 
Cookies: {System.Web.HttpCookieCollection} 
Expires: 0 
ExpiresAbsolute: {1/1/0001 12:00:00 AM} 
Filter: {System.Web.HttpResponseStreamFilterSink} 
HeaderEncoding: {System.Text.UTF8Encoding} 
Headers: 'context.Response.Headers' threw an exception of type 'System.PlatformNotSupportedException' 
IsClientConnected: true 
IsRequestBeingRedirected: false 
Output: {System.Web.HttpWriter} 
OutputStream: {System.Web.HttpResponseStream} 
RedirectLocation: null 
Status: "200 OK" 
StatusCode: 200 
StatusDescription: "OK" 
SubStatusCode: 'context.Response.SubStatusCode' threw an exception of type 'System.PlatformNotSupportedException' 
SuppressContent: false 
TrySkipIisCustomErrors: false 

,当我浏览到localhost/HttpDownloadHandler.ashx而该网站时,不从Silverlight应用程序中启动它 - 保存文件对话显示得很好,这似乎是Silverlight没有正确接受响应头的情况。

有什么可以解决这个问题吗?我愿意改变我当然这样做的方式。

回答

1

响应将发送到Silverlight,而不是web浏览器(所以浏览器不会处理CSV文件并显示文件保存对话框)。您需要直接从Web浏览器发起请求(例如通过JavaScript)。您可以使用Silverlight的HTML/JavaScript桥来轻松完成此操作。

JavaScript桥的一个合理的例子可以找到here

您需要添加一些逻辑是这样的:

HtmlPage.Window.Invoke("startDownload", httpHandlerUrlBuilder.Uri.ToString()); 

然后在JavaScript:

<script type="text/javascript"> 
function startDownload(url){ 
    // you'll probably need to redirect 
    // to a hidden iFrame to actually 
    // kick off the download, by 
    // setting the location to 
    // the url 
    // or ... some other option 
    // there are a number of 
    // different ways. 
} 
</script> 

此外,您也许可以通过HTML DOM做同样的伎俩,从Silverlight的内完全。上面的链接也有关于此的基础知识。

+0

感谢这是一个更好的解决方案,我正在尝试做什么。我最终使用隐藏的iFrame并通过建议的Javascript函数设置其src属性 - 然后使用.Invoke从Silverlight中调用它,并避免完全使用HttpWebRequest。谢谢! – 2010-08-03 16:52:32

1

据我所知,保存对话框将只在按钮的Click事件调用,所以当你收到HTTP响应时,您将无法获得权限打开保存对话框的。

你应该做的是,在你的任何按钮点击事件中,可能是下载按钮,在点击事件中,你应该调用文件对话框,并打开文件流,稍后你会收到Web服务器的响应。