We help IT Professionals succeed at work.

Help needed to store '.wav' files as a blob field in a paradox table.

chunky1
chunky1 asked
on
I need to write an application which will store a .wav file in a blob field of a database.

I have never used the blob field before and need a bit of a helping hand.

Could somebody please supply some sample code or a decent web site link where I can find some code to store and retreive the wav file.

Comment
Watch Question

Commented:
Have you considered storing the file to disk and only store the path\filename to the table? This IMHO is better.

Author

Commented:
Gallaghe,

This option has been mentioned to me but I want to use a blob asit will prevent an in-experienced person from finding the wav files.

Commented:
This is how I do it:

Load the wave to a memorystream.
Store it using a TQuery.

SQL might look like this:
  INSERT INTO TABLE (BLOBFIELDNAME) VALUES(:P0)

TQuery.Params[0].SetBlobData(Stream.Memory, Stream.Size);


When i need to retrive the data i get it AsString, might be a better way of doing this, but it seems to work well enough. then just write the string to disk, or stream or where you might want it.

Sql might look like this:
  SELECT BLOBFIELDNAME FROM TABLE

TQuery.Fields[0].AsString

Hope you understand my gibberish.

Regards
//raidos

Author

Commented:
Gallaghe,

This option has been mentioned to me but I want to use a blob asit will prevent an in-experienced person from finding the wav files.

Commented:
Here is some code taken from another web site;

Peter Lieber
peter.lieber@xenis.cc


The following example demonstrates saving metafile images to a
database as they exist on the disk, preserving any Placeable metafile
headers that may be present in the Metafile. The image is displayed
from the database in a TImage component.

Example:

unit Unit1;

interface

{$IFDEF WIN32}
  uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, Db,
      DBTables;
{$ELSE}
uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, DBTables, DB, Grids, DBGrids, ExtCtrls, StdCtrls;
{$ENDIF}

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Image1: TImage;
    Button1: TButton;
    Table1Name: TStringField;
    Table1WMF: TBlobField;
    OpenDialog1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
  private
    { Private declarations }
    FileName : string; {Used to hold a temp file name}
    procedure LoadWMFFromDatabase; {loads a WMF from the database}
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
{Used for loading metafiles}
  OpenDialog1.Filter := 'Metafiles (*.wmf)|*.wmf';
  OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
  Image1.Stretch := true;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{Erase the temp file if it exists}
  if FileName <> '' then
    DeleteFile(FileName);
end;

{This function gets a temporary file name form the system}
function GetTemporaryFileName : string;
{$IFNDEF WIN32}
  const MAX_PATH = 144;
{$ENDIF}
var
{$IFDEF WIN32}
  lpPathBuffer : PChar;
{$ENDIF}
  lpbuffer : PChar;
begin
{Get the file name buffer}
  GetMem(lpBuffer, MAX_PATH);
{$IFDEF WIN32}
{Get the temp path buffer}
  GetMem(lpPathBuffer, MAX_PATH);
{Get the temp path}
  GetTempPath(MAX_PATH, lpPathBuffer);
{Get the temp file name}
  GetTempFileName(lpPathBuffer,
                  'tmp',
                  0,
                  lpBuffer);
{Free the temp path buffer}
  FreeMem(lpPathBuffer, MAX_PATH);
{$ELSE}
{Get the temp file name}
  GetTempFileName(GetTempDrive('C'),
                  'tmp',
                  0,
                  lpBuffer);
{$ENDIF}
{Create a pascal string containg}
{the  temp file name and return it}
  result := StrPas(lpBuffer);
{Free the file name buffer}
  FreeMem(lpBuffer, MAX_PATH);
end;

procedure TForm1.LoadWMFFromDatabase;
var
  FileStream: TFileStream; {a temp file}
  BlobStream: TBlobStream; {the WMF Blob}
begin
  Image1.Picture.Metafile.Assign(nil);
{Create a blob stream for the WMF blob}
  BlobStream := TBlobStream.Create(Table1WMF, bmRead);
  if BlobStream.Size = 0 then begin
   BlobStream.Free;
   Exit;
  end;
{if we have a temp file then erase it}
  if FileName <> '' then
    DeleteFile(FileName);
{Get a temp file name}
  FileName := GetTemporaryFileName;
{Create a temp file stream}
  FileStream := TFileStream.Create(FileName,
                                   fmCreate or fmOpenWrite);
{Copy the blob to the temp file}
  FileStream.CopyFrom(BlobStream, BlobStream.Size);
{Free the streams}
  FileStream.Free;
  BlobStream.Free;
{Dispaly the image}
  Image1.Picture.Metafile.LoadFromFile(FileName);
end;

{Save a wmf file to the database}
procedure TForm1.Button1Click(Sender: TObject);
var
  FileStream: TFileStream; {to load the wmf file}
  BlobStream: TBlobStream; {to save to the blob}
begin
{Allow the button to repaint}
  Application.ProcessMessages;
  if OpenDialog1.Execute then begin
   {Turn off the button}
    Button1.Enabled := false;
   {Assign the avi file name to read}
    FileStream := TFileStream.Create(OpenDialog1.FileName,
                                     fmOpenRead);
    Table1.Edit;
   {Create a BlobStream for the field Table1WMF}
    BlobStream := TBlobStream.Create(Table1WMF, bmReadWrite);
   {Seek to the Begginning of the stream}
    BlobStream.Seek(0, soFromBeginning);
   {Delete any data that may be there}
    BlobStream.Truncate;
   {Copy from the FileStream to the BlobStream}
    BlobStream.CopyFrom(FileStream, FileStream.Size);
   {Free the streams}
    FileStream.Free;
    BlobStream.Free;
   {Post the record}
    Table1.Post;
   {Load the metafile in to a TImage}
    LoadWMFFromDatabase;
   {Enable the button}
    Button1.Enabled := true;
  end;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  if (Sender as TDataSource).State = dsBrowse then
    LoadWMFFromDatabase;
end;

end.

Code from http://www.delphi3000.com/articles/article_252.asp
Original author
Peter Lieber peter.lieber@xenis.cc
Software Developer
CERTIFIED EXPERT
Commented:
Hello

  here's sample to read and store files to the database, I tested it with wave files and it worked fine, if you have any problems with the code, send me email at mohammed@nasman.com, and i will send you the sample
 
//=====  to save the file to the table
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
     Table1.Append;
     Table1File_Name.Value := OpenDialog1.FileName;
     Table1File_Blob.LoadFromFile(OpenDialog1.FileName);
     Table1.Post;
  end;
end;

//=== to restore the file from the table
procedure TForm1.Button2Click(Sender: TObject);
begin
  SaveDialog1.FileName := Table1['File_Name'];
  if SaveDialog1.Execute then
  begin
    Table1File_Blob.SaveToFile(SaveDialog1.FileName);
  end;
end;

Best regards
Mohammed Nasman

Author

Commented:
Mnasman

Your answer was the easiest to understand and helped me to eventually get towards my target.

Thankyou.

Thanks to all other experts who contributed, but some of the answers were a little too complicated for me.
Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
you are welcome :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.