DEV Community

Peter + AI
Peter + AI

Posted on

πŸ“ Mastering File Loading in Uniface: A Complete Guide to lfileload

When working with Uniface applications, efficiently loading file contents into fields or variables is a common requirement. The lfileload statement provides a powerful and flexible way to accomplish this task. This article explores the functionality based on the Uniface Documentation 10.4, with assistance from AI to ensure comprehensive coverage.

πŸ” What is lfileload?

The lfileload statement copies the contents of a file into a field or variable while ignoring any file redirections in the assignment file. Unlike fileload, which handles network files, lfileload is designed for local file operations.

πŸ“ Basic Syntax

lfileload{/text | /raw | /image | /web} FilePath, Target {, UnicodeFormat | CharSet}
Enter fullscreen mode Exit fullscreen mode

Example:

lfileload "/textfiles/text.txt", TEXTFIELD
Enter fullscreen mode Exit fullscreen mode

🎯 Available Qualifiers

The lfileload statement supports four different qualifiers to handle various file types:

Qualifier Description
/text πŸ”€ Translates raw data to system character set or UnicodeFormat (default behavior)
/image πŸ–ΌοΈ Reads image data and adds initial hash character (#) as indicator
/raw ⚑ Reads raw data without adding hash character or conversion
/web 🌐 Loads files uploaded via browser in Web Application Server components

πŸ“‹ Parameters Breakdown

  • FilePath (String): Name and path of the file (max 255 bytes). Files can be located in zip archives.
  • Target (String): Name of field, variable, or parameter to receive the data.
  • UnicodeFormat (String): Unicode encoding format (UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE)
  • CharSet (String): Character set override for $SYS_CHARSET

πŸ”§ Practical Examples

Loading Text Files

lfileload "/data/config.txt", vConfigData
lfileload/text "/logs/application.log", LOGFIELD, "UTF-8"
Enter fullscreen mode Exit fullscreen mode

Loading Images

lfileload/image "/images/logo.png", IMAGEFIELD
Enter fullscreen mode Exit fullscreen mode

Loading Raw Data

lfileload/raw "/binary/data.bin", RAWDATA
Enter fullscreen mode Exit fullscreen mode

⚠️ Error Handling

Understanding return values is crucial for robust error handling:

$status Return Values

  • β‰₯0: βœ… Number of bytes successfully loaded into Target
  • -1: ❌ I/O error occurred while reading file
  • -4: ❌ Cannot open file

Common $procerror Values

  • -4: File could not be opened
  • -12: File read/write error
  • -16 through -30: Network I/O errors
  • -1101: Invalid field name
  • -1113 through -1117: Various variable/parameter errors

πŸ’‘ Best Practices

  1. Always check return values: Use $status and $procerror for error handling
  2. Choose appropriate qualifiers: Use /text for text files, /image for images, etc.
  3. Handle character encoding: Specify UnicodeFormat when working with international characters
  4. Consider file size limits: FilePath is limited to 255 bytes
  5. Use proper error handling: Implement robust error checking for production applications

πŸŽͺ Conclusion

The lfileload statement is a versatile tool in the Uniface developer's toolkit. By understanding its qualifiers, parameters, and error handling mechanisms, you can efficiently load various file types into your applications. Whether you're working with text files, images, or raw data, lfileload provides the flexibility needed for robust file operations.

Remember to always implement proper error handling and choose the appropriate qualifier for your specific use case. Happy coding! πŸš€

Top comments (0)