SHARE
TWEET

Untitled

a guest Jun 16th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [HttpPost]
  2. [OutputCache(Location = OutputCacheLocation.Client, Duration = 0, VaryByParam = "id")]
  3. [Dependency(typeof(ReportDialogScriptBuilder))]
  4. public ActionResult Run()
  5. {            
  6.     ActionResult result = null;
  7.  
  8.     // parse the content type
  9.     ReportContentType reportContentType = GetRequestValue(ReportFilterType.ReportContentType).ToEnumeration<ReportContentType>();
  10.  
  11.     // parse the report id
  12.     int reportId = GetRequestValue(ReportFilterType.ReportType).ToInt32();            
  13.  
  14.     // declare file extension
  15.     string extension = null;
  16.  
  17.     ContentType contentType = ContentType.HTML;
  18.  
  19.     // determine the content type
  20.     switch (reportContentType)
  21.     {
  22.         case ReportContentType.PDF:
  23.             contentType = ContentType.PDF;
  24.             extension = "pdf";
  25.             break;
  26.         case ReportContentType.Excel:
  27.             contentType = ContentType.XLS;
  28.             extension = "xls";
  29.             break;
  30.         case ReportContentType.Word:
  31.             contentType = ContentType.DOC;
  32.             extension = "doc";
  33.             break;
  34.         case ReportContentType.HTML:
  35.         default:
  36.             break;
  37.     }
  38.  
  39.     var reportData = this.DataContextProvider.Reporting.GetReport(reportId).SingleOrDefault();
  40.     if (null == reportData)
  41.         throw new SystemException("The specified report does not exist.");
  42.  
  43.     RunModes runModes = (RunModes)reportData.RunMode;
  44.  
  45.     // check whether this report uses a custom HTML report writer
  46.     if (runModes.HasFlag(RunModes.Writer))
  47.     {
  48.         StringBuilder sb = new StringBuilder();                
  49.         var sections = DataContextProvider.Reporting.GetReportSections(reportID: reportId);
  50.         foreach (var section in sections)
  51.         {
  52.             // ASSUMPTION: When multiple sections are defined, the individual writers will be implemented to open / close tags correctly (e.g. <HTML>, <BODY>, etc.)
  53.             if (!String.IsNullOrEmpty(section.Header))
  54.             {
  55.                 sb.AppendLine("<h2>" + TextBlock.Create(section.Header) + "</h2>");
  56.             }
  57.  
  58.             // get the IHtmlMarkupWriter / Writer-derived formatter type name
  59.             string typeName = section.FormatterType;
  60.             Type type = Type.GetType(typeName);
  61.             if (null != type)
  62.             {
  63.                 IHtmlMarkupWriter writer = Activator.CreateInstance(type) as IHtmlMarkupWriter;
  64.                 if (null != writer)
  65.                 {
  66.                     var properties = type.GetProperties();
  67.                     foreach (var property in properties)
  68.                     {
  69.                         var attribs = Reflection.ReflectionUtility.GetAttributesOfType<Reporting.ReportFilterTypeAttribute>(property);
  70.                         foreach (var attrib in attribs)
  71.                         {
  72.                             if (null != attrib)
  73.                             {
  74.                                 string value = Resolver.GetReportFilter(attrib.Type);
  75.                                 object raw = ConversionUtility.ChangeType(value, property.PropertyType);
  76.                                 property.SetValue(writer, raw, null);
  77.                                 // break on the first non-null value that is set
  78.                                 if (null != raw) break;
  79.                             }
  80.                         }
  81.                     }
  82.  
  83.                     sb.AppendLine(writer.Render().ToHtmlString());
  84.                 }
  85.             }
  86.         }
  87.  
  88.         switch (contentType)
  89.         {
  90.             case ContentType.DOC:
  91.                 FileContentResult fcr = File(fileContents: Encoding.UTF8.GetBytes(sb.ToString()),
  92.                 contentType: contentType.ToAbbreviation<ContentType>(),
  93.                 fileDownloadName: reportData.Name + ".doc");
  94.                 result = fcr;
  95.                 break;
  96.             case ContentType.HTML:
  97.             default:
  98.                 ContentResult cr = new ContentResult();
  99.                 cr.Content = sb.ToString();
  100.                 cr.ContentType = contentType.ToAbbreviation<ContentType>();
  101.                 cr.ContentEncoding = System.Text.Encoding.UTF8;
  102.                 result = cr;
  103.                 break;
  104.         }                
  105.     }
  106.     // check to see if rendering directly to file or to web control
  107.     else if (String.IsNullOrEmpty(extension))
  108.     {
  109.         // render using ReportViewer as HTML
  110.         string url = "/Reports/Viewer.aspx?" + Request.QueryString.ToString();
  111.         result = new RedirectResult(url);
  112.     }
  113.     else
  114.     {
  115.         // render directly to specified content type
  116.         try
  117.         {
  118.             ReportHelper helper = new ReportHelper(reportId, Resolver);
  119.  
  120.             byte[] renderedBytes = helper.Render(reportContentType,
  121.                 height: reportData.Height,
  122.                 width: reportData.Width,
  123.                 marginBottom: reportData.BottomMargin,
  124.                 marginLeft: reportData.LeftMargin,
  125.                 marginRight: reportData.RightMargin,
  126.                 marginTop: reportData.TopMargin);
  127.  
  128.             // check to see if a user-specified filename should be used
  129.             string name = GetRequestValue(ReportFilterType.ReportFileName);
  130.  
  131.             // check the name value
  132.             if (String.IsNullOrEmpty(name))
  133.             {
  134.                 // get the default download name based on the report type
  135.                 name = ReportUtility.GetDownloadName(reportId);
  136.             }
  137.  
  138.             // format the download file name
  139.             string fileName = String.Format("{0}.{1}", name, extension);
  140.  
  141.             result = File(renderedBytes, helper.ContentType, fileName);
  142.         }
  143.         catch (Exception ex)
  144.         {
  145.             HandleException(ex);
  146.             result = Error();
  147.         }
  148.     }
  149.  
  150.     return result;
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top