Bfiles Datatypes

See Metalink Notes:
47740.1 = Oracle8: LOBs
66046.1 = Oracle8i: LOBs
2096698.6 = ORACLE 8 BFILE EXAMPLE
66312.1 = How to Display Images Stored in BFILE Columns
1048617.6 = QUESTIONS ON BFILES AND HOW TO ACCESS THEM
68016.1 = Browsing database image objects
61737.1 = How to Manipulate Large Objects Using DBMS_LOB Package
66431.1 = LOBS - Storage, Redo and Performance Issues
104652.1 = The InterMedia 8.1.6 README Documentation

Create the table
create table demo
( id        int primary key,
 theBlob    bfile
)
/

Created the directory holding the files:
create directory temp as 'd:\temp';

Insert Rows
insert into demo values(1111,BFILENAME(‘TEMP’,’image.txt’));
Suggestion: Use UPPERCASE to insert or select rows from the table.

Read data
You need to write one function -- something like "blobtochar" in the following:

create or replace
function blobtochar( p_blob in bfile ) return varchar2
as
    l_raw raw(4000);
    l_bfile bfile default p_blob;
    l_vc_buffer varchar2(2000);
begin
    dbms_lob.fileopen( l_bfile );
    l_raw := dbms_lob.substr( l_bfile,4000,1 );
   -- o l_bfile := bfilename( 'TEMP', 'image.txt' );
    dbms_lob.fileclose( l_bfile );
    -- - if it is really TEXT data you can convert it:
    return utl_raw.cast_to_varchar2(l_raw);
   -- or l_vc_buffer varchar2(2000) := utl_raw.cast_to_varchar2(l_raw);
end;
/
 

column x format a40 word_wrapped
select id, blobtochar(theBlob) x from demo
/
        ID X
---------- ----------------------------------------
         1 text from the file;
 

The following example would give you a procedure to call to load a file into the database. It also shows you how to create a BFILE as it created one temporarily to load the file. I could have just inserted l_bfile instead of writing l_blob if I wanted the files "external" from the database.

create or replace procedure load_a_file( p_file in varchar2 )
as
    l_blob  blob;
    l_bfile bfile;
begin
    insert into demo values ( 1, empty_blob() )
    returning theBlob into l_blob;
    l_bfile := bfilename( 'TEMP', p_file );
    dbms_lob.fileopen( l_bfile );
    dbms_lob.loadfromfile( l_blob, l_bfile,
                           dbms_lob.getlength( l_bfile ) );
    dbms_lob.fileclose( l_bfile );
end;
/
 

select field1, field2, dbms_lob.getlength(bfile_field) from samplebfile;

EMPNO ENAME      DBMS_LOB.GETLENGTH(PHOTO)
------ ---------- -------------------------
     2 Vero                           11127
 

Another Method to read the filename
create or replace function filename( p_bfile in bfile )
return varchar2
as
   l_dir  varchar2(255);
   l_name varchar2(255);
   l_return varchar2(1000);
begin
    dbms_lob.filegetname( p_bfile, l_dir, l_name );
    select directory_path || '/' || l_name into l_return
          from dba_directories
         where directory_name = l_dir;
        return l_return;
end;
/

select field1, field2, filename(bfile_field) from samplebfile;

EMPNO ENAME FILENAME(PHOTO)
---------- ---------- --------------------------------------------
           2       Vero d:\temp/Bfiles2.txt