3

I am trying to upload an image file from Apex File browser, as image files are huge so I am using a Chunked upload technique to upload an image into the database. Everything is working fine as the chunked file is being used in Apex Collections to pass in ajax call back process to update in the database. Here is my code in javascript to Upload image as chunked

var fileInputElem = document.getElementById('P130_FILE_UPLOAD');
var fileIndex = 0;

// builds a js array from long string
function clob2Array(clob, size, array) {
  loopCount = Math.floor(clob.length / size) + 1;
  for (var i = 0; i < loopCount; i++) {
    array.push(clob.slice(size * i, size * (i + 1)));
  }
  return array;
}

// converts binaryArray to base64 string
function binaryArray2base64(int8Array) {
  var data = "";
  var bytes = new Uint8Array(int8Array);
  var length = bytes.byteLength;
  for (var i = 0; i < length; i++) {
    data += String.fromCharCode(bytes[i]);
  }
  return btoa(data);
}

// a recursive function that calls itself to upload multiple files synchronously
function uploadFile(pFileIndex) {
  var file = fileInputElem.files[pFileIndex];
  var reader = new FileReader();

  reader.onload = (function(pFile) {
    return function(e) {
      if (pFile) {
        var base64 = binaryArray2base64(e.target.result);
        var f01Array = [];
        f01Array = clob2Array(base64, 30000, f01Array);

        apex.server.process(
          'UPLOAD_FILE',
          {
            x01: file.name,
            x02: file.type,
            f01: f01Array
          },
          {
            dataType: 'json',
            success: function(data) {
              if (data.result == 'success') {
                fileIndex++;

                if (fileIndex < fileInputElem.files.length) {
                  // start uploading the next file
                  uploadFile(fileIndex);
                } else {
                  // all files have been uploaded at this point
                  spinner.stop();
                  fileInputElem.value = '';
                  $('#uploadedFilesRpt').trigger('apexrefresh');
                }
              } else {
                alert('Oops! Something went terribly wrong. Please try again or contact your application administrator.');
              }
            }
          }
        );
      }
    }
  })(file);
  reader.readAsArrayBuffer(file);
}

// variables for spin.js
var spinner;
var spinTargetElem = document.getElementById('wwvFlowForm');
var spinOptions = {
  lines: 13
, length: 28
, width: 14
, radius: 42
, scale: 1
, corners: 1
, color: '#000'
, opacity: 0.25
, rotate: 0
, direction: 1
, speed: 1
, trail: 60
, fps: 20
, zIndex: 2e9
, className: 'spinner'
, top: '50%'
, left: '50%'
, shadow: false
, hwaccel: false
, position: 'absolute'
}

Ajax Call back to Process at the Server level

declare
  lco_collection_name constant apex_collections.collection_name%type := 'UPLOADED_FILES';
  l_blob blob;
  l_filename varchar2(200);
  l_mime_type varchar2(200);
  l_token varchar2(32000);
begin  
  l_filename := apex_application.g_x01;
  l_mime_type := nvl(apex_application.g_x02, 'application/octet-stream');

  -- build BLOB from f01 30k array (base64 encoded)
  dbms_lob.createtemporary(l_blob, false, dbms_lob.session);
  for i in 1 .. apex_application.g_f01.count loop
    l_token := wwv_flow.g_f01(i);
    if length(l_token) > 0 then
      dbms_lob.append(
        dest_lob => l_blob,
        src_lob => to_blob(utl_encode.base64_decode(utl_raw.cast_to_raw(l_token)))
      );
    end if;
  end loop;

  -- add collection member (only if BLOB is not null)
  if dbms_lob.getlength(l_blob) is not null then
    apex_collection.add_member(
      p_collection_name => lco_collection_name,
      p_c001 => l_filename,
      p_c002 => l_mime_type,
      p_blob001 => l_blob
    );
  end if;

  apex_json.open_object;
  apex_json.write(
    p_name => 'result',
    p_value => 'success'
  );
  apex_json.close_object;
exception
  when others then
    apex_json.open_object;
    apex_json.write(
      p_name => 'result',
      p_value => 'fail'
    );
    apex_json.close_object;
end;

The only thing I want to resize the uploaded image like if the size is more than 800x600 then it should be resized to 800x600 at client-side, not on the server-side. Please suggest some best way how I can resize an image in my javascript code.

Goran Kutlaca
  • 2,014
  • 1
  • 12
  • 18
Bilal
  • 41
  • 2
  • Have you seen this? https://jsfiddle.net/codeandcloud/xor3L8db/ – Dan McGhan Sep 13 '19 at 19:45
  • How i can update my current code to use this resize as i am using apex file browser not html file upload – Bilal Sep 13 '19 at 19:49
  • Well, you're using an APEX file browse item, but not in the traditional sense. Normally, the end-user would select the file and submit the page. Then APEX would do the heavy lifting and move the file where it's configured to. In your case, you're using JavaScript and Ajax to move the file in with more control. The idea would be to update your JavaScript code to incorporate some of the logic from the post I showed you. I'm assuming the user clicks a button that invokes `uploadFile`, no? Just add the additional JavaScript code there. – Dan McGhan Sep 14 '19 at 00:17
  • Can you show an example – Bilal Sep 14 '19 at 13:53
  • Perhaps, but it will have to wait until after Oracle OpenWorld... :) In the meantime, have a look at this plug-in to see if it can help: https://apex.world/ords/f?p=100:710:::::P710_PLG_ID:CA.MAXIMET.APEXIMAGECROPPER – Dan McGhan Sep 14 '19 at 15:47
  • can i even resize image to 800x600 using this plugin – Bilal Sep 14 '19 at 17:05
  • @McGhan are you back from OpenWorld ? – Bilal Sep 18 '19 at 19:40
  • Are you trying to resize the image when the client looks at the image on a page, or when the client downloads it to his computer? – YotamW Constantini Dec 12 '19 at 10:20

0 Answers0