当从Silverlight调用时,HttpHandler响应完整事件

时间:2014-08-19 14:45:44

标签: asp.net .net silverlight mvvm httphandler

我有一个位于ASP.NET后面的Silverlight 5 MVVM应用程序。 SL中有一个按钮连接到VM中的ICommand,它在单击时调用特定文件的导出:

public class ReportViewModel : IViewModel

    ............

    private void RunReport()
    {
            var uriPath = string.Format(@"Handlers/ReportHandler.ashx");
            var reportDateString = ReportDate.ToString("yyyy-MM-dd");
            var documentName = String.Format("FinalReport.{0}", reportDateString);
            var uri = new Uri(uriPath, UriKind.Relative);

            var linkToDownload =
                string.Format(
                    "/{0}?reportName={1}&date={2}",
                    uri,
                    documentName,
                    reportDateString
                );

            var finalUri = HtmlPage.Document.DocumentUri.ToString() + linkToDownload;
            HtmlPage.Window.Navigate(new Uri(finalUri));
    }

    ............

}

这很有效,因为它会触发报告的调用,这可能需要一段时间,用户可以继续在UI中工作。 Handler看起来像这样:

public class ReportHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        var reportName = context.Request.QueryString["reportName"];
        var dateString = context.Request.QueryString["date"];
        var limitString = context.Request.QueryString["dataLimit"];

        DateTime date;
        if (!DateTime.TryParse(dateString, out date))
            return;

        var functions = new ExcelReportFunctions();
        var bytes = functions.GetSummaryReport(date);

        if (bytes != null && bytes.Length > 0)
        {
            context.Response.Clear();
            context.Response.Expires = -1;
            context.Response.Buffer = true;
            context.Response.Charset = "";
            context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            context.Response.AddHeader("content-disposition", "attachment;filename=" + reportName.Replace(" ", "%20") + ".xlsx");
            context.Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            context.Response.BinaryWrite(bytes);
            context.Response.Flush();
        }
        else
        {
            context.Response.ContentType = "application/octet-stream";
            throw new Exception();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

但我想要做的是在生成报告时禁用按钮,但我不确定如何最好地将其挂钩。我可以在Silverlight中使用HttpWebRequest但是那时需要处理文件(这是公平的)通过silverlight向用户输出而不是直接响应,这似乎效率低下。

感谢您的帮助。

0 个答案:

没有答案