从 FastReport 中的 Stream 创建的文件加载报告

Load report from file created from Stream in FastReport

有一种生成报告的方法。它在服务器上生成流,然后将其保存到 tmp 文件。

public virtual void Build(ReportType reportType, ReportParameters parameters)
{
        Export = reportType.Id == MaterialReportIds.WarehouseBalanceDynamic;

        var tempFileName = Path.GetTempFileName();
        try {
            ServiceManager<IMaterialsReportsDataService>.Invoke(service =>
            {
                using (var stream = service.BuildReport(reportType, parameters)) {
                    using (var fileStream = File.Create(tempFileName)) {
                        int buf;
                        while ((buf = stream.ReadByte()) >= 0) {
                            fileStream.WriteByte((byte)buf);
                        }
                        fileStream.Flush();
                        fileStream.Close();
                        stream.Close();
                    }
                }
            });

            Report.LoadPrepared(tempFileName);
        }
        finally {
            File.Delete(tempFileName);
        }
}

服务中:

public Stream BuildReport(ReportType reportType, ReportParameters parameters) {
        IReport report = new ReportFactory(Assembly.GetExecutingAssembly()).CreateReport(reportType);
        try {
            report.Build(reportType, parameters);
            MemoryStream stream = new MemoryStream();
            (report.Object as Stream)?.CopyTo(stream);
            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        } finally {
            (report?.Object as IDisposable)?.Dispose();
        }
    }

之后我们使用 Report.LoadPrepared(path to a tmp file) 并且它起作用了,但是当我尝试使用 Report.Load(path to a tmp file) 时它不起作用。有人知道为什么吗?错误说文件格式不正确,但为什么 Report.LoadPrepared() 有效?

只是根据 FastReport 提供的文档猜测 here

Load 方法接受“.frx”文件:

report.Load("report1.frx");

另一方面LoadPrepared接受“.fpx”文件:

Report.LoadPrepared(this.Server.MapPath("~/App_Data/Prepared.fpx"));