Search code examples
jpegclobutl-file

How do I use Oracle utl_file to write an image clob


I have an Oracle Apex application which generates automated eMails. In Apex, the user inserts a JPG image into a rich text field. That image is saved into a CLOB field. When the stored procedure is called, it reads the JPG image and stores it into a local variable called l_image_clob. The program sends the embedded image (note: this an embedded image and it is not an eMail attachment) along with the rest of the eMail body to a list of users. That's all working fine.
Now I'm attempting to save the contents of the JPG image stored in l_image_clob to a JPG file on the windows server. The following code produces a file, named properly and the size is correct, but it isn't readable by the system. I get the error "this is not a valid bitmap file" when I try to open it with Microsoft Paint. How to I use utl_file to do this?

Here's the code which creates the file that is "not a valid bitmap file"
      -- Create a file based on the content of l_image_clob
      l_image_filename := 'image_' || p_event_pkey || '_' || i ||
      '.' || l_image_ext;
      l_file_handle := utl_file.fopen(l_dirname , l_image_filename, 'wb');
      -- wb is write byte. This returns file handle
      <<inner_loop>>
      for i in 1 .. ceil( length( l_image_clob ) / chnksz )
      loop
        utl_file.put_raw( l_file_handle, 
          utl_raw.cast_to_raw( substr( l_image_clob, (i-1) * chnksz + 1, chnksz )));
        utl_file.fflush(l_file_handle);
      end loop inner_loop; 
      utl_file.fclose(l_file_handle); 

Thanks for looking at this.


Solution

  • I found an answer. The Apex-initiated image was base64 encoded. Therefore I had to Decode it. Someone helped me with a procedure to do this. My modified code now looks like this:

    -- Create a file based on the content of l_image_clob
    l_image_filename := 'image_' || p_event_pkey || '_' || i ||
    '.' || l_image_ext;
    clob_base64_to_file(l_image_clob, l_dirname, l_image_filename);
    

    The procedure that's called is as follows:

    create or replace procedure clob_base64_to_file( 
      p_clob        in  clob, 
      p_dir         in  varchar2, 
      p_filename    in  varchar2
      )
    is
      t_buffer          varchar2(32767);
      t_pos             number := 1;
      t_len             number;
      t_fh              utl_file.file_type;
      t_size            number := nls_charset_decl_len( 32764, 
                        nls_charset_id( 'char_cs' ) );
    begin
      t_fh := utl_file.fopen( p_dir, p_filename, 'wb', 32767 );
      t_len := length( p_clob );
      loop
        exit when t_pos > t_len;
        t_buffer := replace( replace( substr( p_clob, t_pos, t_size ), 
                    chr(10) ), chr(13) );
        t_pos := t_pos + t_size;
        while t_pos <= t_len and mod( length( t_buffer ), 4 ) > 0
        loop
          t_buffer := t_buffer || replace( replace( substr( p_clob, t_pos, 1 ), 
                    chr(10) ), chr(13) );
          t_pos := t_pos + 1;
        end loop;
        utl_file.put_raw( t_fh, 
          utl_encode.base64_decode( utl_raw.cast_to_raw( t_buffer ) ) );
      end loop;
      utl_file.fclose( t_fh );
    end;
    

    When I call the clob_base64_to_file procedure it decodes the image and creates a file based on the directory and filename I supply in the call.