Monday, 12 October 2015

Multiple File Upload in Oracle APEX

This method supports the ability for file inputs to accept and upload multiple files into database.

Step 1:
Create a File Browse Item.
Step 2:
In HTML Form Element Attributes set as Multiple”. For Selecting the Multiple Files at a time.
Step 3:
Create the Textfield to get the Filename.
Step 4:
In Dynamic Action “onchange” of File Item. Place the Javascript to get the Selected File Names.   
Code:
var x = document.getElementById("P37_FILE");
vlength=x.files.length;
var txt = "";
$x("P37_FILENAME").value="";
if ('files' in x) {
for (var i = 0; i <vlength; i++) {
if (x.files.length == 0) {
            txt = "Select one or more files.";
            $x("P37_FILENAME").value="";
        } 
else{
                txt += "<br><strong>" + (i+1) + ". file</strong><br>";
                 console.log("txt ="+txt );
                var file = x.files[i];
                if ('name' in file) {
                    txt += "name: " + file.name + "<br>";
                   $x("P37_FILENAME").value+=file.name;
                }
                if ('size' in file) {
                    txt += "size: " + file.size + " bytes <br>";
                }
if(i!=vlength-1)
{
          $x("P37_FILENAME").value+=":";
}
}
            }
}
Step 5:
Create the Page Process to get the file from APEX_APPLICATION_FILES and Store it in a Temporary Table. 
Code:
DECLARE
   l_selected    apex_application_global.vc_arr2;
   lv_filename   VARCHAR2 (100);
BEGIN
   l_selected := apex_util.string_to_table (:p37_filename, ':');
   FOR i IN 1 .. l_selected.COUNT
   LOOP
      lv_filename := l_selected (i);
      BEGIN
         INSERT INTO mul_file_attach
                     (file_id, filename, mime_type, file_content,file_size)
            SELECT ID, lv_filename, mime_type, blob_content,DOC_SIZE
              FROM apex_application_files
             WHERE filename = lv_filename AND created_by = :app_user;
         COMMIT;
      EXCEPTION
         WHEN OTHERS
         THEN
            raise_application_error (-20585, 'Error in Insertion' || SQLERRM);
      END;
      DELETE FROM apex_application_files
            WHERE filename = lv_filename AND created_by = :app_user;
      COMMIT;
   END LOOP;
END;

6 comments:

  1. How to insert multiple files into apex_application_files table?

    ReplyDelete
  2. Any random file among multiple selected files getting uploaded to apex_application_files table and others are rejected...Please help

    ReplyDelete
  3. Hi
    Which apex version is this? I can't see the MULTIPLE option for file browse item in apex 4.2x

    ReplyDelete
  4. Stndgely enough I can see the files going to apex internal table but only the first one goes into my custom table while the others are not.
    Any ideas?

    ReplyDelete
  5. hi This is not updating multiple files

    thanks,
    Madhu

    ReplyDelete