0

Is it possible to convert a blob type image stored in a database table to png? I need to send the image as png to a website using REST, but if I send the database blob item(converted to base64 before sending), the data is getting sent, but the image won't load up when I click on it to show. Could anyone give me a tip on how to do this?

Thanks in advance, Tamas

Edit1: here's the whole code I'm using

The first request uploads the file to the website, and sends back a token with which I can then attach the file to the second request. As you can see, both of the request bodys state that the content type is image/png, so I don't exactly understand what can be the problem.

    create or replace procedure publish_error_tickets(p_priority_id varchar2, p_description varchar2)
    is
      req1 utl_http.req;
      res1 utl_http.resp;
      req2 utl_http.req;
      res2 utl_http.resp;
      lc_entire_message clob;
      l_attachment clob;
      url1 varchar2(4000) := 'http://revprox.local/inno-dev.cloudapp.net/redmine/uploads.json';
      url2 varchar2(4000) := 'http://revprox.local/inno-dev.cloudapp.net/redmine/projects/support-interface-teszt/issues.json';

      name varchar2(4000);
      token varchar2(4000);
      buffer varchar2(4000); 
      content varchar2(4000);
      p_id number;
      p_image blob;
      l_request_body clob;
      l_request_body_length number;

      l_offset number := 1;
      l_amount number := 2000;
      l_buffer varchar2(4000);
    begin

      select max(id) into p_id from t_tkt_temp_images;
      select image into p_image from t_tkt_temp_images where id = p_id;


       l_request_body := apex_web_service.blob2clobbase64(p_image);

  l_request_body_length := length(l_request_body);

  req1 := utl_http.begin_request(url1, 'POST',' HTTP/1.1');
  utl_http.set_header(req1, 'User-Agent', 'mozilla/4.0'); 
  utl_http.set_header(req1, 'Content-Type', 'application/octet-stream'); 
  utl_http.set_header(req1, 'Content-Length', l_request_body_length);
  utl_http.set_header(req1, 'Content-Disposition', 'attachment; filename="file.png"');
      utl_http.set_header(req1, 'X-Redmine-API-Key', '0asddq3t23w4esdf');

      while l_offset < l_request_body_length loop
        dbms_lob.read(l_request_body, l_amount, l_offset, l_buffer);
        utl_http.write_text(req1, l_buffer);
        l_offset := l_offset + l_amount;
      end loop;

      res1 := utl_http.get_response(req1);

      begin
        loop
          utl_http.read_text(res1, buffer);
          lc_entire_message := buffer;
        end loop;

        utl_http.end_response(res1);
      exception
        when utl_http.end_of_body 
        then
          utl_http.end_response(res1);
      end;

      apex_json.parse(lc_entire_message);
      token := apex_json.get_varchar2(p_path => 'upload.token');

      content := 
      '{
      "issue": {
        "subject": "Hibajegy",
        "priority_id": '|| 1 ||',
        "description": "asd",
        "uploads": [
          {"token": "'||token||'", "filename": "file.png", "content_type": "image/png"}
        ]
        }
      }';

      req2 := utl_http.begin_request(url2, 'POST',' HTTP/1.1');
      utl_http.set_header(req2, 'user-agent', 'mozilla/4.0'); 
      utl_http.set_header(req2, 'content-type', 'application/json'); 
      utl_http.set_header(req2, 'Content-Length', length(content));
      utl_http.set_header(req2, 'X-Redmine-API-Key', '0asddq3t23w4esdf');

      utl_http.write_text(req2, content);
      res2 := utl_http.get_response(req2);

      begin
        loop
          utl_http.read_line(res2, buffer);
        end loop;

        utl_http.end_response(res2);
      exception
        when utl_http.end_of_body 
        then
          utl_http.end_response(res2);
      end;
    end publish_error_tickets;
Hleb
  • 7,037
  • 12
  • 58
  • 117
Spigy
  • 41
  • 1
  • 8
  • What format is the image data in currently? Instead of sending it in base64 you might be better off sending a URL instead. c.f. http://stackoverflow.com/questions/14415398/best-approach-to-design-a-rest-web-service-with-binary-data-to-be-consumed-from which *might* be helpful to you. – Jeffrey Kemp Sep 02 '16 at 15:26
  • If you mean it's mimetype, it is saved as a png. By the way, the uploading process looks like this: I have to send the image data first to the website via http, then I'll receive a token with which I can attach the file to the rest message. I asked on another page, and someone there suggested I set the mimetype to png in the http header. Is that even possible? – Spigy Sep 05 '16 at 08:11
  • Yes, you send the header `Content-Type: image/png` – Jeffrey Kemp Sep 05 '16 at 13:08
  • I added the code to my original question, could you take a look at it? Maybe you can figure out something. – Spigy Sep 06 '16 at 13:22
  • Problems: (1) you haven't changed your content type, it's still `application/octet-stream`; also, it's often case-sensitive, so it should be `Content-Type` not `content-type`; (3) you have added it as well to your `Content-Disposition` header for some reason, this is not a valid parameter to Content-Disposition. – Jeffrey Kemp Sep 07 '16 at 00:25
  • I would put Content-Disposition as a header as well, not part of the body, for consistency. – Jeffrey Kemp Sep 07 '16 at 00:26
  • It wasn't exactly my code, I just used scraps I found on the net, thanks for the tips though, I corrected them. For the first request I have to use octet-stream as content-type, otherwise the website wouldn't consume the request, but as you can see in the second request's body I stated it that the file I'm attaching is image/png. My best guess right now is that by sending the file as octet-stream, the image won't be displayed in the web browser, because it's still binary data, but the problem with that is I'm not able to download it to check on a computer. – Spigy Sep 08 '16 at 08:19
  • I don't know what you expect the server to be doing with a Content-Disposition header with a "Content-Type" **parameter**! `Content-Type` is a **http header**, not a parameter. – Jeffrey Kemp Sep 08 '16 at 12:36
  • Yes, as I said it wasn't my code, if i've known what exactly it does I wouldn't write it like that, but I'm still a bit unfamiliar with this stuff. – Spigy Sep 08 '16 at 15:19
  • You're maintaining it, so it is YOUR code now :) you need to learn to understand it in full, otherwise you will always be reliant on the kindness of strangers on SO. – Jeffrey Kemp Sep 08 '16 at 23:41
  • Can't argue with that. :) – Spigy Sep 10 '16 at 07:15

0 Answers0