ADF BC REST service - Attachments

JDev: 12.2.1.3.0
Source: 1) ADF BC REST, 2) Oracle JET application

Continuting on the previous discussion on ADF BC REST - Type Mapping - the framework can be stretched further to work with uploading files and images. Working with attachments is an important aspect in any commercial application; and ADF BC REST service provides us with a robust mechanism to store and display files and images through BLOB columns.

The following article discusses both about developing the REST service for storing documents in an Oracle database, as well as configuring an Oracle JET application which uploads and displays an image on the GUI.



The REST API

I have added a PROFILE_IMAGE column of BLOB type to the employees table in the HR schema, and I will use this column for storing my attachments. BLOB columns are handled in a slightly different way by the REST API. The BLOB column is not included in the actual payload, but it comes as part of the links section, as an enclosure attribute.


The href value can be directly set into the src attribute of an image tag, and the data will be shown on the UI.

If we look further, we also see a requestType: image/png for the ProfileImage attribute. By default, the request-type is application/octet-stream for a BLOB column, but if you want to restrict the user to upload only images for a column, you may define a custom contentType property for the view attribute.



As shown in the resource description, we may use a PATCH/PUT operation to upload the content to this column. A great feature provided by ADF BC is to convert an uploaded file into a Base64 String, and the same can be streamed via a PATCH or PUT REST operation.

{
      "ProfileImage": "YOUR_BASE64_STRING_GENERATED_FROM_IMAGE"
}

The JET UI

There are two steps to upload the image into the database column:
1) The first step is to convert the image into a Base64 string.


2) The second step is to use a patch operation to send this data via the REST call. Please note that since the BLOB attribute is not part of the REST payload, I have not used JET's common model to perform the patch operation. I have used jQuery's ajax call to do this.


Once the success callback function is executed, we construct the href url and set the same for the avatar's src attribute.

If the href returns a 404 error code, I simply set the initials of the employee on the avatar component.



Cheers!

Comments