Oracle Forms is primarily designed for data entry and transaction processing, but modern business applications often require interaction with the client file system. For example, a user may need to upload a CSV file into the application, or save a generated report to their local machine. Direct file system access is not possible in standard Oracle Forms due to security restrictions, but Oracle provides a utility called WebUtil that extends Forms’ capabilities.
In this tutorial, we will explore how to read and write files from Oracle Forms using WebUtil, with examples and best practices to make your application more powerful and user-friendly.
What is WebUtil in Oracle Forms?
WebUtil is a library provided by Oracle that allows Oracle Forms applications to interact with the client machine. It acts as a bridge between the Forms runtime environment and the client’s operating system.
Some of the key features of WebUtil include:
- File upload and download between client and server.
- Reading and writing text files on the client machine.
- Accessing client-side environment variables.
- Invoking host commands on the client.
- Accessing clipboard, registry, and other client resources.
For file handling specifically, WebUtil provides built-in PL/SQL APIs to perform read and write operations.
How to Enable WebUtil in Oracle Forms
Before working with files, ensure that WebUtil is properly configured:
- Install WebUtil: Verify that the WebUtil library (webutil.pll and webutil.olb) is available in your Forms environment.
- Compile and Attach: Compile
webutil.pll
intowebutil.plx
and attach the library to your form. - Configure FormsWeb.cfg: Enable WebUtil by adding:
WebUtilArchive=frmwebutil.jar,jacob.jar WebUtilLogging=on WebUtilLoggingDetail=normal WebUtilBlockInput=on WebUtilDispatchMonitorInterval=5
- Grant Permissions: Ensure the Java security policy grants permissions for file access.
- Run in Browser: Test with a small function, like displaying the client username, to confirm WebUtil is working.
Reading Files Using WebUtil
To read a file from the client machine, you can use the WebUtil_File package. This is especially useful for reading configuration files, importing CSV data, or verifying text logs.
Example: Reading a Text File
DECLARE
v_file_id Client_Text_IO.File_Type;
v_line VARCHAR2(2000);
BEGIN
v_file_id := Client_Text_IO.Fopen('C:\Users\Test\input.txt', 'r');
LOOP
Client_Text_IO.Get_Line(v_file_id, v_line);
MESSAGE('Line: ' || v_line);
SYNCHRONIZE;
END LOOP;
EXCEPTION
WHEN Client_Text_IO.ENDOFFILE THEN
Client_Text_IO.Fclose(v_file_id);
MESSAGE('File reading completed.');
WHEN OTHERS THEN
MESSAGE('Error reading file: ' || SQLERRM);
END;
This code opens a text file from the client machine and reads it line by line.
Writing Files Using WebUtil
WebUtil also allows writing data to files on the client machine. This can be used to generate logs, reports, or export data.
Example: Writing to a Text File
DECLARE
v_file_id Client_Text_IO.File_Type;
BEGIN
v_file_id := Client_Text_IO.Fopen('C:\Users\Test\output.txt', 'w');
Client_Text_IO.Put_Line(v_file_id, 'This is a test line.');
Client_Text_IO.Put_Line(v_file_id, 'Oracle Forms WebUtil writing demo.');
Client_Text_IO.Fclose(v_file_id);
MESSAGE('File written successfully.');
EXCEPTION
WHEN OTHERS THEN
MESSAGE('Error writing file: ' || SQLERRM);
END;
This creates (or overwrites) a file and writes text lines into it.
Uploading and Downloading Files with WebUtil
In addition to direct read/write, WebUtil provides methods to transfer files between client and server.
- Download from Server to Client
DECLARE
v_success BOOLEAN;
BEGIN
v_success := WebUtil_File.Transfer_File_To_Client('C:\Users\Test\invoice.pdf',
'C:\forms_data\invoice.pdf');
IF v_success THEN
MESSAGE('File downloaded successfully.');
ELSE
MESSAGE('Download failed.');
END IF;
END;
- Upload from Client to Server
DECLARE
v_success BOOLEAN;
BEGIN
v_success := WebUtil_File.Transfer_File_From_Client('C:\Users\Test\upload.csv',
'C:\forms_data\upload.csv');
IF v_success THEN
MESSAGE('File uploaded successfully.');
ELSE
MESSAGE('Upload failed.');
END IF;
END;
This is extremely useful when dealing with reports, scanned documents, or importing/exporting CSV/Excel data.
Best Practices for File Handling with WebUtil
- Validate file paths before reading or writing to prevent errors.
- Handle exceptions gracefully using
WHEN OTHERS
blocks. - Restrict file types (e.g., only
.txt
or.csv
) for security reasons. - Avoid hardcoding paths; allow users to choose files via WebUtil file chooser dialogs.
- Use non-blocking operations when dealing with large files.
- Secure server directories to prevent unauthorized file access.
Conclusion
WebUtil makes Oracle Forms much more powerful by enabling interaction with the client machine. By using WebUtil APIs, developers can read files, write files, upload documents, and download reports seamlessly. This bridges the gap between traditional Forms applications and modern business requirements.
With proper configuration and security in place, WebUtil can significantly improve usability and efficiency in Oracle Forms applications.
The post How to Read and Write Files from Oracle Forms Using WebUtil appeared first on Vinish.Dev.