【问题标题】:Download report file which is to be generated in to client side下载要生成的报告文件到客户端
【发布时间】:2018-08-20 07:53:43
【问题描述】:

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

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

【问题讨论】:

  • 在服务器端生成文件然后给客户端一个文件的链接是没有错的,只要在文件下载后的某个时间点有删除报告的任务.确保生成文件的目录是临时目录,并且文件名不能被猜到。
  • @Neil,是的。你是对的。目前我正在使用这种方法。但是我只想知道天气可以直接下载到客户端吗。
  • 最后一行:result.DocumentBytesStreambyte[],无论哪种方式,都可以转换为IFileResult 并直接传递给客户端。
  • @Neil 我使用与您建议的类似方式完成了任务。谢谢。

标签: c# reporting telerik-reporting


【解决方案1】:

我可以通过使用返回类型 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);
        }
    }

【讨论】:

  • 你还在写一个文件(实际上每次都是同一个文件,所以如果同时有2个请求进来,一个会覆盖另一个)。您可以使用内存流绕过文件写入。
猜你喜欢
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多