[firebase-br] Armazenar arquivos pdf no banco
marcelo luiz stefaniak - teflamar
marcelo em teflamar.com.br
Ter Jun 27 08:48:23 -03 2006
bom dia maycon,
acredito hoje, que gravando dentro do banco de dados seja a melhor maneira,
ainda mais se vc tiver vários arquivos (acima de 1000).
gravando fora do banco, este fica menor, mas a pasta fica cheia de arquivos,
e seu acesso, tanto quanto seu gerenciamente fica lento.
segue um exemplo, com 2 maneiras de salvar e recuperar dados do/para blob.
sds marcelo luiz stefaniak-teflamar
unit blob1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, IBDatabase, IBCustomDataSet, IBQuery, Grids,
DBGrids, OleCtnrs,
dbtables,
jpeg, ExtCtrls;
type
TForm1 = class(TForm)
btgravar1: TButton;
Edit1: TEdit;
Label1: TLabel;
btrecuperar1: TButton;
DBGrid1: TDBGrid;
gravar2: TButton;
q2: TIBQuery;
IBDatabase1: TIBDatabase;
IBTransaction1: TIBTransaction;
q2COD: TIntegerField;
q2NOME: TIBStringField;
q2BLOB1: TBlobField;
q2NOME_ARQ: TIBStringField;
DBGrid2: TDBGrid;
DataSource1: TDataSource;
btabrir: TButton;
Button1: TButton;
btrecuperar2: TButton;
Image1: TImage;
procedure btgravar1Click(Sender: TObject);
procedure btrecuperar1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure gravar2Click(Sender: TObject);
procedure btabrirClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure btrecuperar2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
d : tibdatabase;
q : TibQuery;
t : tibtransaction;
s : TDataSource;
implementation
uses util;
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
d:=tibdatabase.create(self);
d.Params.loadfromfile('params.txt');
d.LoginPrompt:=false;
d.DatabaseName:='d:\temp\dados.fdb';
d.connected:=true;
t:=tibtransaction.create(self);
t.DefaultDatabase:=d;
d.DefaultTransaction:=t;
q:=TibQuery.Create(self);
q.Database:=d;
q.SQL.text:='select * from teste';
q.open;
s:=tdatasource.create(self);
s.DataSet:=q;
dbgrid1.DataSource:=s;
end;
procedure TForm1.btgravar1Click(Sender: TObject);
begin
t.StartTransaction;
q.sql.text:='INSERT INTO TESTE (COD,NOME,NOME_ARQ,BLOB1) VALUES
(0,:NOME,:NOME_ARQ,:BLOB1)';
try
q.parambyname('NOME').asstring:='Teste '+datetostr(now);
q.parambyname('NOME_ARQ').asstring:=edit1.text;
q.ParamByName('BLOB1').LoadFromFile(edit1.text,ftBlob);
q.ExecSQL;
t.Commit;
except
on e:exception do
begin
ShowMessage(e.message);
end;
end;
end;
procedure TForm1.btrecuperar1Click(Sender: TObject);
var blob: TBlobStream;
begin
//blob := q.CreateBlobStream(q.fieldByName('BLOB1') as tblobfield,
bmRead);
//Blob := TBlobStream.Create(q.Fieldbyname('blob1') as TBlobField,
bmRead);
//Blob:= TBlobStream.Create(q.Fieldbyname('blob1'), bmReadWrite);
//Blob:= TBlobStream.Create(q.FieldByName('blob1') as tblobfield,
bmReadWrite);
//Blob:= TBlobStream.Create(q2blob1, bmReadWrite);
Blob := TBlobStream.Create(q2.Fieldbyname('blob1') as TBlobField,
bmReadwrite);
try
blob.Seek(0, soFromBeginning);
with TFileStream.Create('c:\temp\'+q2.fieldbyname('nome_arq').asstring,
fmCreate) do
try
CopyFrom(blob, blob.Size)
finally
Free
end;
finally
blob.Free
end;
end;
procedure TForm1.gravar2Click(Sender: TObject);
var
fs : TFileStream;
begin
try
fs := TFileStream.Create(edit1.text, fmOpenRead or fmShareDenyWrite);
if t.InTransaction then t.commit;
t.StartTransaction;
q.sql.text:='INSERT INTO TESTE (COD,NOME,NOME_ARQ,BLOB1) VALUES
(0,:NOME,:NOME_ARQ,:BLOB1)';
q.parambyname('NOME').asstring:='Teste '+datetostr(now);
q.parambyname('NOME_ARQ').asstring:=edit1.text;
q.ParamByName('BLOB1').LoadFromstream(fs,ftBlob);
q.ExecSQL;
t.Commit;
except
on e:exception do
begin
ShowMessage(e.message);
end;
end;
fs.Free
end;
procedure TForm1.btabrirClick(Sender: TObject);
begin
q2.active:=not q2.active;
end;
procedure TForm1.Button1Click(Sender: TObject);
var FileStream: TFileStream; {a temp file}
BlobStream: TBlobStream; {the WMF Blob}
filename:string;
begin
{Create a blob stream for the WMF blob}
BlobStream := TBlobStream.Create(q2blob1, bmRead);
if BlobStream.Size = 0 then begin
BlobStream.Free;
Exit;
end;
FileName := q2nome_arq.asstring;
{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;
procedure TForm1.btrecuperar2Click(Sender: TObject);
var
warquivo :string;
BlobStream : TStream;
FileStream : TFileStream;
begin
filestream:=nil;
blobstream:=nil;
warquivo:='c:\temp\'+q2.fieldbyname('nome_arq').asstring;
try
BlobStream := q2.CreateBlobStream(q2.FieldByName('blob1'),bmRead);
FileStream := TFileStream.Create(warquivo, fmCreate or fmOpenWrite);
FileStream.CopyFrom(BlobStream, 0);
finally
blobstream.free;
filestream.Free;
execfile(warquivo);
// Liberando memoria
end;
end;
end.
Mais detalhes sobre a lista de discussão lista