File Upload Improvements in APEX 5.1
Updated 10/10/2017 now that APEX 5.1 has been out for a while.
Updated 22/8/2020 with some corrections.
The standard File Upload item type is getting a nice little upgrade in Apex 5.1. By simply changing attributes on the item, you can allow users to select multiple files (from a single directory) at the same time.
In addition, you can now restrict the type of file they may choose, according to the MIME type of the file, e.g.
image/jpg. This file type restriction can use a wildcard, e.g.
image/*, and can have multiple patterns separated by commas, e.g.
NOTE (22/8/2020): this doesn’t stop a user from choosing files of other types (they can change the file type selector) and doesn’t validate the file types.
Normally, to access the file that was uploaded you would query
APEX_APPLICATION_TEMP_FILES with a predicate like
name = :P1_FILE_ITEM. If multiple files are allowed, however, the item will be set to a colon-delimited list of names, so the suggested code to get the files is:
declare arr apex_global.vc_arr2; begin arr := apex_util.string_to_table(:P1_MULTIPLE_FILES); for i in 1..arr.count loop select t.whatever into your_variable from apex_application_temp_files t where t.name = arr(i); end loop; end;
You can play with a simple demo here: https://apex.oracle.com/pls/apex/f?p=UPLOAD_DEMO&c=JK64 . (UPDATE 10/10/2017: recreated demo on apex.oracle.com) If you want to install the demo app yourself, you may copy it from here.
If you want to support drag-and-drop, image copy&paste, load large files asynchronously, restrict the actual file type or maximum file size that may be uploaded, you will probably want to consider a plugin instead, like Daniel Hochleitner’s DropZone.