Skip to Main Content
  • Questions
  • Uploading File to a OCI Instance via Oracle APEX using PL/SQL

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Afraim.

Asked: September 04, 2023 - 7:27 am UTC

Last updated: September 07, 2023 - 12:07 pm UTC

Version: 23.1

Viewed 1000+ times

You Asked

Hi Experts,

I am fairly new to Oracle Apex. My req was basically to make a OCI instance with a program which lets me upload files to it. I was able to succesfully do that, but then i needed to make a Simple UI. I was asked to use oracle Apex for that. Thinking it worked easily in postman i tried to implement it with the help of chat gpt and a few mentors. but no matter what we do. it does not work. i know im not providing clarity but i dont know what else to say.
Without much knowledge in PL/SQL, I have created a simple code to upload the file. which i will post here. My OCI program keeps giving me errors regarding the headers and 'Required part 'File' is missing'. It should be a minor error and i will appreciate any help. incase u need more data, feel free to ask
DECLARE
    l_response CLOB;
    l_file_content CLOB;
BEGIN
    l_file_content := :P8_FILE;

 

    -- apex_web_service.g_request_headers(1).name := 'Content-Type';
    -- apex_web_service.g_request_headers(1).value := 'application/x-www-form-urlencoded';
    apex_web_service.g_request_headers(1).name := 'Content-Type';
    -- apex_web_service.g_request_headers(2).Value := 'multipart/form-data';
    apex_web_service.g_request_headers(1).Value := 'multipart/form-data; boundary=' || 'APEX' || DBMS_RANDOM.STRING('X', 10);

 

    l_response := apex_web_service.make_rest_request(
        p_url => 'http://<MY_IP_ADDRESS>:8080/uploadfile',
        p_http_method => 'POST',
        -- p_body => 'file='||l_file_content
        p_body => 'file=' || UTL_RAW.CAST_TO_VARCHAR2(Utl_encode.text_encode( UTL_FILE.FREAD(UTL_FILE.FOPEN('UPLOAD_DIRECTORY', 'DataLoader.xlsx', 'R')), 'UTF-8', Utl_encode.BASE64)) || '&other_param=value'
    );

 


    -- Process the response or set a page item with the response
    :P8_RESPONSE := l_response;
EXCEPTION 
    WHEN OTHERS THEN
        -- Handle errors
        :P8_RESPONSE := 'Error at file upload: ' || SQLERRM;
END;

NOTE: i have hidden my ip address.
I apologize for any mistakes or improper way in communication, im not very social. Please consider

and Chris said...

First up, the requirement "Create a program to upload files to is ambiguous". There are lots of unanswered questions like:

Where do the files come from originally?
How exactly are the end users supposed to upload them?
Is this a one-off process or will it be a regular job?

These depend on your environment and your employer's/client's needs - Chat GPT and similar can't help you answer these questions. You need to speak with the people who set the requirements to clarify their goals.

That said, when creating an APEX app to allow you (& others?) to upload files I doubt you're being asked to build a REST API on your machine that APEX calls. I suspect some form of drag & drop interface is what's desired.

APEX has in-built data loading capabilities that enable you to build these pages. The docs explain the process:

https://docs.oracle.com/en/database/oracle/apex/23.1/htmdb/creating-applications-with-data-loading-capability.html#GUID-B0173334-15DD-4EDC-9842-9F661B5C657B

If you really do need to get OCI to read data from your local machine (again: I doubt this is what's needed) via REST, you'll need to configure the networks to enable OCI to talk to your machine. I know very little about this; you may find the following links useful:

https://www.ateam-oracle.com/post/oci-networking-best-practices-recommendations-and-tips---part-one---general-oci-networking
https://docs.oracle.com/en-us/iaas/Content/Network/Concepts/overview.htm

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Afraim, September 05, 2023 - 7:13 am UTC

I apologize for the confusion, let me explain properly what my requirment is.
I am Creating an interface on Oracle APEX for the purpose of client interaction, which means that this will be the UI for Clients. The Flow goes like this. The client is able to download a Template file(Excel file), Then the client can fill thier own content into the template file. Now This file needs to be processed, so we have the program which processes this in a OCI instance. It is a Springboot Program which processes the file from a directory folder called 'Target' inside the OCI instance.
The issue i have is that i cant get the filled template file into the target folder inside the OCI instance. I have written a program which i use to upload the file and exposed its API to the/uploadfile endpoint.

I created a Filebrowser item on Oracle APEX and wrote the process in PL/SQL with the post request and the API URL. Unfortunerly i cant find the issue.

Let me summarize,
the Client intially gets the page to download the template.
They fill in the template
They go to the next page and use the filebrowser item to upload the filled template
They click the Submit/Process button.
The File reaches the OCI instance directory inside the folder named 'Target'



The issue i have is the program i wrote is working properly, that is, uploading the file to the OCI instance via postman and Simple HTML webpage. But Replicating it in Apex is not working and im not sure why


I'm not sure how to answer if it is a one off process or a regular job. i guess it repeats as much the client will require.

I believe the last comment, Uploading a file from the clients local machine from a apex page and calling rest api to bring this file into my cloud OCI instance, is precisely what i need.


I will provide any more data as needed, I have been stuck for a month or two and tried everything i know(even though i am new).

This is the small program i use to upload the file
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class FileUploadController {

    private static final String UPLOAD_FOLDER = "/home/opc/target"; // Replace with your actual upload folder path

    @PostMapping("/uploadfile")
    public String uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:/";
        }

        try {
            byte[] bytes = file.getBytes();
            Path filePath = Paths.get(UPLOAD_FOLDER, file.getOriginalFilename());
            Files.write(filePath, bytes);

            redirectAttributes.addFlashAttribute("message", "File uploaded successfully");
        } catch (IOException e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("message", "Error uploading the file");
        }

        return "redirect:/";
    }
}



Chris Saxon
September 07, 2023 - 12:07 pm UTC

I don't understand where REST API come into this. Have you built an API which writes the file to OCI? Does it process files that are already in the OCI directory?

Either way it sounds like the file upload page in APEX needs to call this API when the user submits it. I don't know enough about APEX or understand your requirements well enough to give further guidance.

A reader, September 08, 2023 - 5:45 am UTC

I have created a Springboot Application on my OCI instance which is continuously is waiting for requests on port 8080, The /uploadfile is the endpoint for file upload. The program works fine because i tested it in multiple areas, but something with the pl/sql i posted above seems to have an error. or a core component is missing from my code.

I was hoping someone who has experience with apex could assist with the code.

Thank you for your response, i shall check this thread frequently to see if someone have commented

More to Explore

APEX

Keep your APEX skills fresh by attending their regular Office Hours sessions.