unit uBlob;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, DBCtrls, ComCtrls, ExtDlgs, Db, DBGrids, ABSMain;

type
  TfBlob = class(TForm)
    Panel2: TPanel;
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Panel4: TPanel;
    odBlob: TOpenDialog;
    sdBlob: TSaveDialog;
    btCancel: TButton;
    btOk: TButton;
    lSize: TLabel;
    btLoad: TButton;
    btSave: TButton;
    Label1: TLabel;
    procedure FormShow(Sender: TObject);
    procedure btLoadClick(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure btOkClick(Sender: TObject);
    procedure btCancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    CurrentTable: TABSDataSet;
    CurrentGrid: TDBGrid;
  end;

var
  fBlob: TfBlob;

implementation

{$R *.DFM}

procedure TfBlob.FormShow(Sender: TObject);
var
  BlobStream: TStream;
begin
  CurrentTable := TABSDataset(CurrentGrid.DataSource.DataSet);
  if (not TABSTable(CurrentTable).ReadOnly) then
    CurrentTable.Edit;
  BlobStream := CurrentTable.CreateBlobStream(CurrentGrid.SelectedField,bmRead);
  lSize.Caption := Format('%d bytes', [BlobStream.Size]);
  BlobStream.Free;
  Caption := Format('Blob Field "%s"',[CurrentGrid.SelectedField.FieldName]);
end;

procedure TfBlob.btLoadClick(Sender: TObject);
var
  FileStream: TFileStream;
  BlobStream: TStream;
begin
 if (odBlob.Execute) then
   begin
     BlobStream := CurrentTable.CreateBlobStream(CurrentGrid.SelectedField,bmWrite);
     FileStream := TFileStream.Create(odBlob.FileName,fmOpenRead or fmShareDenyNone);
     BlobStream.CopyFrom(FileStream,FileStream.Size);
     lSize.Caption := Format('%d bytes', [BlobStream.Size]);
     FileStream.Free;
     BlobStream.Free;
   end;
end;

procedure TfBlob.btSaveClick(Sender: TObject);
var
  FileStream: TFileStream;
  BlobStream: TStream;
begin
  if (sdBlob.Execute) then
    begin
      FileStream := TFileStream.Create(sdBlob.FileName,fmCreate);
      BlobStream := CurrentTable.CreateBlobStream(CurrentGrid.SelectedField,bmRead);
      FileStream.CopyFrom(BlobStream,BlobStream.Size);
      BlobStream.Free;
      FileStream.Free;
    end;
end;

procedure TfBlob.btOkClick(Sender: TObject);
begin
 if (not TABSTable(CurrentTable).ReadOnly) then
   CurrentTable.Post;
end;

procedure TfBlob.btCancelClick(Sender: TObject);
begin
 if (not TABSTable(CurrentTable).ReadOnly) then
   CurrentTable.Cancel;
end;

procedure TfBlob.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   if (CurrentTable.State = dsEdit) then
    CurrentTable.Cancel;
end;

end.

