Search code examples
c#reportingtelerik-reporting

Download report file which is to be generated in to client side


I am using the below code to create and download a Telerik report.

var reportName = "../api/Templates/Invoice.trdp";
var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
var reportSource = new Telerik.Reporting.UriReportSource()
{
 Uri = reportName
};
reportSource.Parameters.Add("ID", 3);
reportSource.Parameters.Add("Username", "demouser");
var deviceInfo = new System.Collections.Hashtable()
{
 {"DocumentTitle", "Annual Report" }
};
var result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);
if (!result.HasErrors)
{
 System.IO.File.WriteAllBytes(System.IO.Path.ChangeExtension(reportName, "pdf"), result.DocumentBytes);
}
}

Once I host it in a server, it creates the file in the server side. How can I download it into the client machine without creating any files in the server.


Solution

  • I was able to do it by returning the file back to the client using the return type HttpResponseMessage

    public HttpResponseMessage GenerateOrderReport(int orderID)
        {
            var reportName = ConfigurationManager.AppSettings["EmailAttachmentURLTemplate"];
            string activeDir = ConfigurationManager.AppSettings["EmailAttachmentSaveLocation"];
            string newPath = System.IO.Path.Combine(activeDir, ConfigurationManager.AppSettings["EmailAttachmentSaveFolder"]);
            var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
            var reportSource = new Telerik.Reporting.UriReportSource()
            {
                Uri = reportName
            };
            reportSource.Parameters.Add("OrderID", 141);
            reportSource.Parameters.Add("OrderMethodTypeID", 2);
    
            var deviceInfo = new System.Collections.Hashtable()
            {
                {"DocumentTitle", "Order Report" }
            };
    
            var result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);
            if (!result.HasErrors)
            {
                System.IO.Directory.CreateDirectory(newPath);
                string newFileName = "OrderReport.pdf";
                newPath = System.IO.Path.Combine(newPath, newFileName);
                FileStream fileStream = new FileStream(newPath, FileMode.Create, FileAccess.ReadWrite);
                fileStream.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
                fileStream.Close();
                HttpResponseMessage fileResult = new HttpResponseMessage(HttpStatusCode.OK);
                var stream = new FileStream(newPath, FileMode.Open);
                fileResult.Content = new StreamContent(stream);
                fileResult.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                fileResult.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                fileResult.Content.Headers.ContentDisposition.FileName = newFileName;
                return fileResult;
            }
            else
            {
                throw new Exception("Report contains errors. " + result.Errors[0].Message);
            }
        }