Skip to Main Content

SQL & PL/SQL

upload local jpg file using utl_http

HenkBDec 11 2013

Hi experts,

Plz help....

I have  found in the internet some code for clob and did some small changes

create or replace procedure http_post_binary (

         p_url_in in varchar2

        ,p_data_in in blob

        ,p_data_type in varchar2

       )

as

v_http_req utl_http.req;

v_http_resp utl_http.resp;

v_status_code varchar2(256);

v_reason_phrase varchar2(256);

v_length   binary_integer;

v_buffer          raw (2000);

v_amount          pls_integer := 2000;

v_offset          pls_integer := 1;

v_errm      varchar2(200);

begin

        v_http_req := utl_http.begin_request (p_url_in, 'POST', 'HTTP/1.1');

        utl_http.set_header(v_http_req, 'User-Agent', 'Mozilla/5.0');

        utl_http.set_header(v_http_req, 'content-type', p_data_type);

        v_length := dbms_lob.getlength(p_data_in);

     

       --if message data under 32kb limit

       if v_length <=32767

       then

   

           utl_http.set_header (v_http_req, 'Content-Length', v_length);

           utl_http.write_raw (v_http_req, p_data_in);   

   

       -- If Message data more than 32kb

       elsif v_length>32767

       then

 

        utl_http.set_header (v_http_req, 'Transfer-Encoding', 'chunked');

    

           WHILE (v_offset < v_length)

           LOOP

              DBMS_LOB.read (p_data_in,

                             v_amount,

                             v_offset,

                             v_buffer);

              utl_http.write_raw (v_http_req, v_buffer);

              v_offset := v_offset + v_amount;

          

           END LOOP;

   

       end if;

     

        v_http_resp := utl_http.get_response (v_http_req);

        v_status_code := v_http_resp.status_code;

        v_reason_phrase := v_http_resp.reason_phrase;

         utl_http.end_response(v_http_resp);

         return;

    exception

        when others then

            v_errm := sqlerrm;

            dbms_output.put_line (v_errm);

            raise;

end http_post_binary;

I have a webservice that upload a local binary file (for example a jpg picture) and insert into a Oracle database

my test url http://localhost/UploadService/upload?token={p_token}&filename={p_filename}

Say I have a picture in C:\Users\Peter\Pictures\test_image.jpg -> http://localhost/UploadService/upload?token=5&filename=test_image.jpg

How can I upload this picture using the above procedure. I have seen examples to download a web picture using utl_http and examples for uploading xml/clob using an url.

It would be helpful if there is an example how to upload the image file using my uploadservice. I have made a C# example that works but want to make it work also using utl_http....

database: 10gr2 (added)

Thanks in advance,

Regards

Post Details
Locked on Jan 8 2014
Added on Dec 11 2013
0 comments
492 views