Recent

Author Topic: I can't load a report from a MemoryStream  (Read 350 times)

babycode

  • New Member
  • *
  • Posts: 38
I can't load a report from a MemoryStream
« on: July 16, 2024, 03:08:07 pm »
I'm trying to load a resource from a TMemoryStream using the function:

Code: Pascal  [Select][+][-]
  1. function ExtrairRecursosMemoria(NomeRecurso: String): TStream;
  2. var
  3.   Recurso: TLResource;
  4.   StreamRecurso: TLazarusResourceStream;
  5.   MemStreamRecurso: TMemoryStream;
  6. begin
  7.   Result := nil;
  8.   StreamRecurso := nil;
  9.  
  10.   Recurso := LazarusResources.Find(NomeRecurso);
  11.  
  12.   if Recurso = nil then
  13.   begin
  14.     raise Exception.Create('Recurso com nome "' + NomeRecurso + '" não foi encontrado.');
  15.   end;
  16.  
  17.   begin
  18.     try
  19.       try
  20.         StreamRecurso := TLazarusResourceStream.Create(NomeRecurso, nil);
  21.         MemStreamRecurso := TMemoryStream.Create;
  22.         MemStreamRecurso.CopyFrom(StreamRecurso, StreamRecurso.Size);
  23.         MemStreamRecurso.Position := 0;
  24.         Result := MemStreamRecurso;
  25.       except
  26.             on E: Exception do
  27.             begin
  28.                ShowMessage('Error')
  29.             end;
  30.       end;
  31.     finally
  32.       StreamRecurso.Free;
  33.     end;
  34.   end;
  35. end;

I use the function as follows:
Code: Pascal  [Select][+][-]
  1. var
  2.   ReportStream: TStream;
  3. begin
  4.       ReportStream := ExtrairRecursosMemoria('history');
  5.       try
  6.         try
  7.            ReportStream.Position := 0;
  8.            frlrRelatorioHistorico.LoadFromStream(ReportStream);
  9.            frlrRelatorioHistorico.ShowReport;
  10.          except
  11.             on E: Exception do
  12.             begin
  13.                ShowMessage('Error')
  14.             end;
  15.          end;
  16.       finally
  17.           MemStreamRecurso.Free;
  18.       end;
  19. end;

When I try to extract I get an error "invalid binary report: invalid report version (60)". But if I save the physical file and load it from disk it works normally.
« Last Edit: July 16, 2024, 03:11:09 pm by babycode »

korba812

  • Sr. Member
  • ****
  • Posts: 442
Re: I can't load a report from a MemoryStream
« Reply #1 on: July 16, 2024, 03:22:26 pm »
You should use LoadFromXMLStream method instead of LoadFromStream.

Code: Pascal  [Select][+][-]
  1. frlrRelatorioHistorico.LoadFromXMLStream(ReportStream);
  2.  

babycode

  • New Member
  • *
  • Posts: 38
Re: I can't load a report from a MemoryStream
« Reply #2 on: July 16, 2024, 03:43:49 pm »
 :D ;D

It worked very well! Grateful!

 

TinyPortal © 2005-2018