Jump to content

[ajuda] blob


CanonTG
 Share

Recommended Posts

Boas, quero ler um anexo(seleccionado na minha dbgrid) que está na minha base de dados com o tipo blob.

para adicionar a tabela fiz assim

var
blob: TStream;

begin
//add ficheiro blob
 blob := Query.CreateBlobStream(Query.FieldByName('anexos'), bmWrite);try
 blob.Seek(0, soFromBeginning);
   try
     blob.CopyFrom(TFileStream.Create(Edit3.Text, fmOpenRead), TFileStream.Create(Edit3.Text, fmOpenRead).Size)
   finally
     TFileStream.Create(Edit3.Text, fmOpenRead).Free
   end;
 finally
  blob.Free;
 end;

end;
Link to comment
Share on other sites

Depende do tipo de ficheiro desse anexo.

Numa aplicação que fiz há dois anos para mostrar pdfs que estavam na BD usava este código:

procedure TfMain.GetPDFFromDB(Acro: TWebBrowser; Campo, Fich: string);
var
blob: TStream;
begin
with dm.qMyQuery do
begin
 if Active then
 Close;
 SQL.Clear;
 SQL.Add('SELECT '+Campo+' FROM tabela WHERE condition=:T');
 Params[0].AsInteger := cbT.KeyValue;
 Open;
end;
 if not dm.qMyQuery.IsEmpty then
 begin
 GetTempPath(MAX_PATH, @TempPath);
 blob := dm.qMyQuery.CreateBlobStream(dm.qMyQuery.FieldByName(Campo),bmRead);
 try
	 blob.Seek(0, soFromBeginning);
	 if FileExists(TempPath+Fich) then
	 DeleteFile(TempPath+Fich);
	 with TFileStream.Create(TempPath+Fich, fmCreate) do
	 try
	 CopyFrom(blob, blob.Size);
	 finally
	 Free;
	 end;
 finally
	 blob.Free;
 end;
 Acro.Navigate(TempPath+Fich);
 end;
end;

Provavelmente existe um modo mais elegante e fácil de fazer isto, mas funcionava bem.

Edited by joptech
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.