Potato
Potato

Reputation: 475

Load report from file created from Stream in FastReport

There is a method that builds reports. It generates stream on server and then save it to a tmp file.

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);
        }
}

On service :

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();
        }
    }

After that we use Report.LoadPrepared(path to a tmp file) and it works, but when I try to use Report.Load(path to a tmp file) it doesn't. Do anybody know why? The error says that file format is incorrect, but why Report.LoadPrepared() works?

Upvotes: 0

Views: 3007

Answers (1)

Jevgenij Nekrasov
Jevgenij Nekrasov

Reputation: 2760

Just guessing from the documentation FastReport provided here.

Load method accepts ".frx" files:

report.Load("report1.frx");

From the other hand LoadPrepared accepts ".fpx" files:

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

Upvotes: 1

Related Questions