Board index » delphi » Reading the name of files stored in Blob Fields

Reading the name of files stored in Blob Fields

"Paul D" <pdewhi...@palmsoftware.co.uk> skrev i melding
news:42cc98f7.0111300807.9f5623c@posting.google.com...

Quote
> I know how to store and retrive files from BLOB fields but I want to
> know how to get the name of the file stored inside the blob.

> At the moment I simply store the filename in a seperate text field.

> Can any help ?

You would have to use a blobstream -
first - write a string (the file name) using a TWriter or a similar technique
secondly - write the file.

--
Bjoerge Saether
Asker, Norway
bjorge@hahaha_itte.no (remve the obvious)

 

Re:Reading the name of files stored in Blob Fields


Quote
"Paul D" <pdewhi...@palmsoftware.co.uk> wrote in message

news:42cc98f7.0111300807.9f5623c@posting.google.com...

Quote
> I know how to store and retrive files from BLOB fields but I want to
> know how to get the name of the file stored inside the blob.

The filename isn't usually stored inside the file.

Quote

> At the moment I simply store the filename in a seperate text field.

Sounds like the best way.

Re:Reading the name of files stored in Blob Fields


"Paul D" <pdewhi...@palmsoftware.co.uk> skrev i melding
news:42cc98f7.0112030131.47d015ff@posting.google.com...
Quote
> "Bj?ge S?her" <REMOVE_bsaether@THIS_online.no> wrote in message

<news:iGPN7.2775$yB2.54271@news1.oke.nextra.no>...

Quote
> > "Paul D" <pdewhi...@palmsoftware.co.uk> skrev i melding
> > news:42cc98f7.0111300807.9f5623c@posting.google.com...
> > > I know how to store and retrive files from BLOB fields but I want to
> > > know how to get the name of the file stored inside the blob.

> > > At the moment I simply store the filename in a seperate text field.

> > > Can any help ?

> > You would have to use a blobstream -
> > first - write a string (the file name) using a TWriter or a similar
technique
> > secondly - write the file.

> Do you have any examples ?

Well, it could be done like this:

procedure WriteBLOBField(Field: TField; FileName: string);
var
  MS: TMemoryStream;
  BS: TStream;
  W : TWriter;
begin
  if not (Field is TBlobField) then
    raise Exception.CreateFmt('Field "%s" is not a BLOB field. Procedure
WriteBLOBField failed.', [Field.FieldName]);
  MS:=TMemoryStream.Create;
  try
    MS.LoadFromFile(FileName);
    with TBlobField(Field) do begin
      BS:=DataSet.CreateBlobStream(Field, bmWrite);
      try
        W:=TWriter.Create(BS, 4096);
        try
          W.WriteString(FileName);
          W.Write(MS.Memory^, MS.Size);
        finally
          W.Free;
        end;
      finally
        BS.Free;
      end;
    end;
  finally
    MS.Free;
  end;
end;

function ReadBLOBField(Field: TField): string; // returns filename
var
  MS: TMemoryStream;
  BS: TStream;
  R : TReader;
  FileName: string;
  Buff: Array[0..4095] of byte;
  LeftToRead: integer;
  ReadNow: integer;
begin
  if not (Field is TBlobField) then
    raise Exception.CreateFmt('Field "%s" is not a BLOB field. Function
ReadBLOBField failed.', [Field.FieldName]);
  MS:=TMemoryStream.Create;
  try
    with TBlobField(Field) do begin
      BS:=DataSet.CreateBlobStream(Field, bmRead);
      try
        R:=TReader.Create(BS, 4096);
        try
          FileName:=R.ReadString;
          LeftToRead:=BS.Size-R.Position;
          while LeftToRead > 0 do begin
            if LeftToRead >= SizeOf(Buff) then
              ReadNow:=SizeOf(Buff)
            else
              ReadNow:=LeftToRead;
            R.Read(Buff, ReadNow);
            MS.Write(Buff, ReadNow);
            Dec(LeftToRead, ReadNow);
          end;
          MS.SaveToFile(FileName);
          result:=FileName;
        finally
          R.Free;
        end;
      finally
        BS.Free;
      end;
    end;
  finally
    MS.Free;
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  t1.Open;
  if t1.EOF then
    t1.Insert
  else
    t1.Edit;
  WriteBlobField(t1.FieldByName('DATA'), 'C:\Test.Dat');
  t1.post;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  t1.Open;
  Caption:=ReadBlobField(t1.FieldByName('DATA'));
end;

...it may even be done more elegantly, I haven't been doing this very
often...;-)

--
Bjoerge Saether
Asker, Norway
bjorge@hahaha_itte.no (remve the obvious)

Other Threads