This blog demonstrates how to upload a text file in Oracle APEX and store its contents directly into a page item. Using JavaScript’s file reader API, the file is read on the client side without server upload. Once read, the content is dynamically assigned to a page item, allowing immediate use in forms or processes.
We can approach this in two ways. First, the text file can have a structured format: the first row contains the name with a colon, the second row contains the email with a colon, and the third row contains the remarks with a colon. When the user uploads this kind of text file, the content is read and the corresponding values are set into the appropriate page items.
Second, we can simply upload any text file, and its entire content is set into a single APEX page item.
I created this blog for learning purposes, to explore the different options we can implement using APEX.
Step1: Create Apex Page with Following components.
P3_File Upload Item
P3_Name Text Field
P3_Mail Text Field
P3_Remarks Text Field
P3_Content Text Area
Submit1 Button
Submit2 Button
Step2: For the first button (submit1), add a Dynamic Action on Click and paste the following code
var fileInput = document.getElementById("P3_FILE");
if (fileInput && fileInput.files.length > 0) {
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var lines = e.target.result.split(/\r?\n/);
lines.forEach(function(line) {
var parts = line.split(':'); // split label and value
if(parts.length >= 2) {
var label = parts[0].trim().toLowerCase();
var value = parts.slice(1).join(':').trim(); // in case value has ":"
if(label === 'name') {
$s("P3_NAME", value);
} else if(label === 'email') {
$s("P3_MAIL", value);
} else if(label === 'remarks') {
$s("P3_REMARKS", value);
}
}
});
};
reader.readAsText(file, "UTF-8");
} else {
alert("Please select a file first.");
}
From the above code, you can see that we are setting values to the page items based on the uploaded text document.
Step3: For the first button (submit2), add a Dynamic Action on Click and paste the following code
var fileInput = document.getElementById("P3_FILE");
if (fileInput && fileInput.files.length > 0) {
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function(e) {
$s("P3_CONTENT", e.target.result);
};
reader.readAsText(file, "UTF-8");
} else {
alert("Please select a file first.");
}
From the above code, all the content of the text file is set into the content page item
Step4: Save and run the application.
Comments
Post a Comment