Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#4

AW: Inhalt eines Blob-Feldes als Datei speichern

  Alt 17. Mär 2016, 09:27
Ich tippe mal darauf, dass sich in dem Blob-Feld RichText befindet.

Damit wäre die Endung der Datei auch klar: .rtf

Um Blob-Daten in eine Datei zu speichern bietet sich folgendes an:
Delphi-Quellcode:
unit Unit2;

interface

uses
  System.Classes,
  System.SysUtils;

type
  TStreamData = class abstract( TInterfacedPersistent, IStreamPersist )
  private
    function SupportsStreamPersist( Persistent: TPersistent; var StreamPersist: IStreamPersist ): Boolean;
    procedure LoadFromStreamPersist( const Source: IStreamPersist );
    procedure SaveToStreamPersist( const Dest: IStreamPersist );
  protected
    function GetDataStream( const Writeable: Boolean ): TStream; virtual; abstract;
    procedure AssignTo( Dest: TPersistent ); override;
  public
    procedure Assign( Source: TPersistent ); override;
    procedure LoadFromStream( Stream: TStream );
    procedure SaveToStream( Stream: TStream );
  end;

  TFileStreamData = class( TStreamData )
  private
    FFileName: string;
  protected
    function GetDataStream( const Writeable: Boolean ): TStream; override;
  public
    constructor Create( const FileName: string );
  end;

implementation

{ TStreamData }

procedure TStreamData.Assign( Source: TPersistent );
var
  lSource: IStreamPersist;
begin
  if SupportsStreamPersist( Source, lSource )
  then
    LoadFromStreamPersist( lSource )
  else
    inherited;
end;

procedure TStreamData.AssignTo( Dest: TPersistent );
var
  lDest: IStreamPersist;
begin
  if SupportsStreamPersist( Dest, lDest )
  then
    SaveToStreamPersist( lDest )
  else
    inherited;
end;

procedure TStreamData.LoadFromStream( Stream: TStream );
var
  lDest: TStream;
begin
  lDest := GetDataStream( true );
  try
    lDest.CopyFrom( Stream, -1 );
  finally
    lDest.Free;
  end;
end;

procedure TStreamData.LoadFromStreamPersist( const Source: IStreamPersist );
var
  lDest: TStream;
begin
  lDest := GetDataStream( true );
  try
    Source.SaveToStream( lDest );
  finally
    lDest.Free;
  end;
end;

procedure TStreamData.SaveToStream( Stream: TStream );
var
  lSource: TStream;
begin
  lSource := GetDataStream( false );
  try
    Stream.CopyFrom( lSource, -1 );
  finally
    lSource.Free;
  end;
end;

procedure TStreamData.SaveToStreamPersist( const Dest: IStreamPersist );
var
  lSource: TStream;
begin
  lSource := GetDataStream( false );
  try
    Dest.LoadFromStream( lSource );
  finally
    lSource.Free;
  end;
end;

function TStreamData.SupportsStreamPersist( Persistent: TPersistent; var StreamPersist: IStreamPersist ): Boolean;
begin
  Result := Supports( Persistent, IStreamPersist, StreamPersist );
end;

{ TFileStreamData }

constructor TFileStreamData.Create( const FileName: string );
begin
  inherited Create;
  FFileName := ExpandFileName( FileName );
end;

function TFileStreamData.GetDataStream( const Writeable: Boolean ): TStream;
begin
  if Writeable
  then
    begin
      ForceDirectories( ExtractFilePath( FFileName ) );
      Result := TFileStream.Create( FFileName, fmCreate or fmOpenWrite or fmShareDenyRead );
    end
  else
    Result := TFileStream.Create( FFileName, fmOpenRead or fmShareDenyWrite );
end;

end.
Den Blob-Inhalt bekommt man jetzt wie folgt auf die Platte:
Delphi-Quellcode:
procedure foo;
var
  dst: TStreamData;
begin
  dst := TFileStreamData.Create( 'testdaten.rtf' );
  try
    dst.Assign( IrgendeineQuery.FieldByName( 'BlobFeldName' ) );
  finally
    dst.Free;
  end;
end;
Kürzer habe ich es nicht hinbekommen
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat