Saturday, November 13, 2010

how to load os file into blob field ?

If you want to to load a file (i.e. text file, image file, pdf file etc.) into
a blob field, then follow the following procedure.

1. create a database directory :
create or replace database directory image_test_dir
as 'd:\db_dir\'

2. Use dbms_Lob package to load os file into blob;

declare

v_file_loc bfile;

v_Image_size integer;

v_img blob;

begin

--v_file_loc := bfilename('IMAGE_TEST_DIR', 'testRpt.pdf');
v_file_loc := bfilename('IMAGE_TEST_DIR', 'front.tif');

v_image_size := DBMS_LOB.GETLENGTH(v_file_Loc);

dbms_output.put_line(v_image_size);

insert into image_upload(id, img)
values (1, empty_blob()) returning img into v_img;

dbms_Lob.open(v_file_Loc, DBMS_LOB.FILE_READONLY );

DBMS_LOB.LOADFROMFILE( v_img,
v_file_loc,
v_image_size );

DBMS_LOB.CLOSE(v_file_Loc);

commit;


exception
when others then dbms_output.put_line('Exception : ' || sqlerrm);

end;

No comments:

Post a Comment