節省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