我正在使用以下代码创建和下载Telerik报告。
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);
}
}
一旦将其托管在服务器中,它将在服务器端创建文件。如何在不在服务器中创建任何文件的情况下将其下载到客户端计算机中。
答案 0 :(得分:0)
我能够通过使用返回类型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);
}
}