Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Converting from a BASE64 BLOB

Andrew TulleyAug 9 2012 — edited Aug 10 2012
I have a BLOB column which contains BASE64 encoded jpegs.

I want to decode these from BASE64 such that I can get the original JPEG.

I've written a function to do this (based on an example found here but modified slightly since my source is a BLOB not a CLOB: http://blogs.dayneo.co.za/2012/07/base64-decode-using-plsql.html)

	FUNCTION base64_decode(p_file IN BLOB) 
	RETURN BLOB IS
	
	 l_BASE64_LN_LENGTH constant pls_integer := 30000;
	 l_result           blob := empty_blob();
	 l_pos              number := 1;
	 l_amount           number;
	 l_buffer           raw(32767);
	 l_rbuf             raw(32767);
	 l_length           pls_integer;
	
	begin
	
	 DBMS_LOB.createTemporary(l_result, true, dbms_lob.CALL);
	 
	 l_length  := DBMS_LOB.getLength(p_file);
	 
	 while l_pos <= l_length loop
	
	  l_amount := l_BASE64_LN_LENGTH;
	  
	  dbms_lob.read(p_file, l_amount, l_pos, l_rbuf);
	  
	  l_buffer := utl_encode.base64_decode(l_rbuf);
	
	  dbms_lob.writeappend(l_result, 
	                       utl_raw.length(l_buffer), 
	                       l_buffer);
	 
	  l_pos    := l_pos + l_BASE64_LN_LENGTH;
	
	 end loop;
	
	 return l_result;
	 
	END base64_decode;
This works absolutely fine where the input BLOB (p_file) is less than 30000 bytes i.e. I can save the generated BLOB and open it in a graphics application.

However, if the input BLOB (p_file) is larger than 30000 bytes, when I save the resultant BLOB (i.e. l_result) and try to open it with a graphics application such as Windows Picture Viewer I get the message "Drawing failed".



Any thoughts, comments or suggestions would be very much appreciated.

Thanks,
Andy
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 7 2012
Added on Aug 9 2012
2 comments
791 views