将要生成的报告文件下载到客户端

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

我正在使用以下代码创建和下载 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);
}
}

一旦我将其托管在服务器中,它就会在服务器端创建文件。如何在不在服务器中创建任何文件的情况下将其下载到客户端计算机。

我可以通过 return 在文件中使用 return 类型 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);
        }
    }