using System; using System.Configuration; using System.Web; using DevExpress.XtraPrinting; using DevExpress.XtraReports.UI; using System.Collections.Generic; using Neo.Afx.Services.Reports; using System.IO; using System.Linq; using Neo.Afx.Security; namespace Neo.LegitimateLicences.Web.AfxServices { public class ReportDownloader : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.QueryString.Count == 0) return; var reportDefinitionID = -1L; var database = new ReportDatabase(); if (!Int64.TryParse(context.Request.QueryString["reportDefinitionID"], out reportDefinitionID)) return; var reportDefinition = database.GetReportDefinitionWithServer(reportDefinitionID); var assembly = reportDefinition.Server.Url; var typename = assembly + "." + reportDefinition.ReportFolder + "." + reportDefinition.ReportName; var temp = Type.GetType(typename + ", " + assembly); var report = (XtraReport)Activator.CreateInstance(Type.GetType(typename + ", " + assembly)); var reportParameters = context.Request.QueryString.AllKeys.Where(qs => !qs.Contains("reportDefinitionID") && !qs.Contains("reportType")).Select(qs => new KeyValuePair(qs, context.Request.QueryString[qs])).ToList(); foreach (var p in report.Parameters) { if (p.Description.ToLower() == "userid") { p.Value = UserProfile.Current.UserID; } else { var parameter = reportParameters.Where(a => a.Key.ToLower() == p.Name.ToLower()).FirstOrDefault(); if (parameter.Key != null) { p.Value = parameter.Value; } } } var fileName = (context.Request.QueryString["fileName"] != null) ? context.Request.QueryString["fileName"].ToString() : ""; if (string.IsNullOrEmpty(fileName)) { fileName = reportDefinition.ReportName + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf"; } MemoryStream localStream = new MemoryStream(); report.ExportToPdf(localStream); #region Push Data context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.AddHeader("Content-Disposition", " attachment; filename=" + fileName); context.Response.AddHeader("Content-Length", localStream.Length.ToString()); context.Response.ContentType = "application/pdf"; context.Response.Charset = "UTF-8"; context.Response.BinaryWrite(localStream.ToArray()); context.Response.Flush(); context.Response.Clear(); #endregion } public bool IsReusable { get { return false; } } } }