节省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没有正确接受响应头的情况。
有什么可以解决这个问题吗?我愿意改变我当然这样做的方式。
感谢这是一个更好的解决方案,我正在尝试做什么。我最终使用隐藏的iFrame并通过建议的Javascript函数设置其src属性 - 然后使用.Invoke从Silverlight中调用它,并避免完全使用HttpWebRequest。谢谢! – 2010-08-03 16:52:32