Subject Re: [firebird-support] UDF for external file from/to blob?
Author Teträm Corp
using Delphi:

function SaveBlobToFile(Path, FileName: PChar; Blob: PBlob): Integer;
cdecl; export;
var
buffer: array of Char;
LongueurLue: Integer;
BlobS: TMemoryStream;
begin
try
FileName := PChar(Path + ValidFileName(FileName));
BlobS := TMemoryStream.Create;
with BlobS do try
Seek(0, soBeginning);
if Assigned(Blob.BlobHandle) and (Blob.SegmentCount > 0) then begin
SetLength(buffer, Blob.MaxSegmentLength);
while LongBool(Blob.GetSegment(Blob.BlobHandle, @buffer[0],
Blob.MaxSegmentLength, LongueurLue)) do
BlobS.Write(buffer[0], LongueurLue);

ForceDirectories(Path);
if FileExists(FileName) then DeleteFile(FileName);
BlobS.SaveToFile(FileName);
end;
Result := BlobS.Size;
finally
Free;
end;
except Result := 0;
end; // pour être sûr de ne pas faire planter le serveur !!!!
end;

procedure LoadBlobFromFile(Path, FileName: PChar; Blob: PBlob); cdecl;
export;
var
buffer: array[0..4095] of Char;
FS: TFileStream;
begin
try
FileName := PChar(Path + ValidFileName(FileName));
if not FileExists(FileName) then Exit;

FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
with FS do try
while Position < Size do
Blob.PutSegment(Blob.BlobHandle, @buffer[0], Read(buffer,
Length(buffer)));
finally
Free;
end;
except
end; // pour être sûr de ne pas faire planter le serveur !!!!
end;


Bob Murdoch a écrit :
> I'm using FB1.5, and looking for a UDF that would allow me to load an
> external file into a blob, or write an external file from a blob. I
> have found a couple of UDF's on the IBPhoenix site that relate to
> external files, but don't have the functionality outlined above.
>
> For instance:
>
> create table Report (
> report_id integer not null,
> contents blob);
>
> insert into Report
> (report_id, contents)
> values
> (1, ReadFile('c:\temp\Report.xls');
>
> select
> report_id, WriteFile(contents, 'c:\temp\Report2.xls')
> from
> report
>
>
> I could do without the WriteFile at the moment, but need a quick way
> to insert some old files into the database without writing a UI to do
> it.
>
> tia,
>
> Bob M..
>