File Plugin Basics
File Plugin Basics Guide
Last updated
Was this helpful?
File Plugin Basics Guide
Last updated
Was this helpful?
The File Plugin in Servoy provides server-side access to file system operations when working with browser clients and can also be used on NGDesktop for client-side file access. With the plugin, developers can handle file operations such as retrieving metadata, reading/writing text or binary data, uploading and downloading files, and performing file system operations like moving, copying, and tracking files.
For NGDesktop applications, where client-side file access is required, you can use specific features that provide access to the local file system directly. You can find relevant documentation on NGDesktop file.
More information about File Plugin can be found . More information about JSFile can be found .
You can retrieve various file metadata such as the file path, size, and last modified time.
Code Example: Get File Metadata
Here is a simple example that creates a file helloworld.txt
in the user folder of user "servoy" in Windows:
Back slash in windows file paths:
In the example above the path C:\users\servoy\helloworld.txt
is written as C:\\users\\servoy\\helloworld.txt
(with double back slashes). This is done because the \
is the escape character in javascript strings.
Another option is to use /
instead, this will work just as well:
Here an example to create a file in the home directory of user "servoy" in Ubuntu:
To do the same on a Mac:
Here is a sample that does the same as the previous examples, but it works on all the operating systems above:
Code Example: Writing Text to a File
Code Example: Reading Text from a File
Memory Considerations When working with large text files, it's important to consider memory usage, as reading large files entirely into memory could exhaust available resources. For larger files, consider processing them line by line.
When using readTXTFile
, the entire text is written to the memory. With big text files this might be a problem, because they take a lot of memory, maybe even more than available.
Because of this problem, using readTXTFile
is not the best option if to read huge files. In that case it is better to use some inline java code to read the file line by line using a buffered reader. See this example:
Code Example: Writing Binary Data
Code Example: Reading Binary Data
If only a part of the file has to be read, the number of bytes can be specified using the second parameter. For example if 1kB should be read:
Memory Considerations When reading or writing large binary files, you must manage memory carefully to avoid heap space issues. Consider breaking large files into smaller chunks and processing them in parts.
Code Example: Writing XML Data to a File
Code Example: Downloading a File
Code Example: Uploading Files
Servoy also provides dedicated UI components for file uploading, allowing more customization and flexibility in handling user uploads. These components offer features such as multiple file selection and additional styling options, making them ideal for applications with more complex file upload requirements.
Code Example: Listing Files in a Directory
Code Example: Moving Files by Name
Code Example: Moving Files with JSFile
Instances
Code Examples: Copying Files Based on File Names: You can copy files using their file paths as string values.
Copying Files Based on JSFile Instances: Alternatively, you can copy files by first creating JSFile objects.
Code Example:
After executing this code, there will be a new (empty) file in the temp directory with a name like this: helloworld1571360175321712533.txt
. The filename will be unique because of the number that is added in the middle.
Usage Scenario: If your application creates temporary files (such as log files, data exports, or reports) that are needed during the session but not afterward, this method allows you to automatically delete these files when the solution closes.
Code Example:
To create a file, there should be a first. Creating JSFiles can be done using the function . On a JSFile there is the function to actually create the file on disk.
OS-independent path: As can be seen in the examples above, it is not a good practice to use hardcoded paths in the code, because they don't work on every OS. It is better to use a more general approach. The file plugin has several functions to help with that: and .
The method is used to write content to files in text format. You can specify the file where the text will be written, the text content itself, and optionally the encoding format. By default, the system uses the operating system's default encoding, but you can override this by specifying a different encoding if needed.
Reading from a text file can be done using the method.
Optionally the charset can be specified in the second parameter, if left out it will take the OS default (For a list of possible charsets, look ):
If the user should choose which file to read, can be called without parameters, then a file choose dialog will open:
To write binary data to a file, you can use method by passing a byte array.
The plugins.file.writeFile
method allows you to write binary data (such as images, PDFs, or any other file in binary format) to a file. This is useful when working with binary data from databases or other sources that need to be saved as files on the server or client-side.
You can read binary files with the method.
The plugins.file.readFile
method allows you to read the contents of binary files, such as images, PDFs, or any other file that contains non-text data. This method returns the binary data as a byte array, which can then be processed or stored in a database.
If the user should choose which file to read, can be called without parameters, then a file choose dialog will open:
In Servoy, you can write XML files using the method. This allows you to store structured data in XML format, which is commonly used for configurations, data interchange, or documents.
Servoy allows you to trigger file downloads or open files directly in the browser. This can be done using method.
File uploads in Servoy are handled using the method. Uploaded files can be processed server-side.
Default Upload Location The servoy.FileServerService.defaultFolder
property in Server Plugins
section of determines where files are uploaded by default on the server. You can change this property to suit your application's needs.
: This component allows users to upload individual files with a user-friendly interface. It can be customized to match the application's design and offers events like file selection and upload progress tracking.
: This component enables users to upload multiple files at once, providing a streamlined experience for applications that require bulk file uploads. It supports multiple file selection, drag-and-drop functionality, and file size validations.
To list files in a directory, use method. The plugins.file.getFolderContents
method retrieves an array of JSFile instances representing the files and directories within a specified folder. This method is useful for obtaining the list of files from a directory, and it supports various filters to narrow down the content based on file names, file types, visibility, and lock status.
You can move files using the method.
The plugins.file.moveFile
method allows you to move files from one location to another. You can move files based on their file names (as strings) or by using JSFile instances. The method returns true if the move is successful and false if any error occurs during the process.
The method allows you to copy files from one location to another. You can use this method to duplicate files based on file names or JSFile instances. The method returns true if the copy operation is successful, and false if any error occurs during the process.
Servoy provides the ability to create temporary files that are automatically managed and removed when the client session ends. These temp files are useful for handling data that is only needed during the session and should not persist after the solution is closed. Temp files will be created in the default temporary-file directory, using the given prefix and suffix to generate its name. Temporary files can be created using method.
The method in Servoy ensures that a file is automatically deleted when the client’s solution is closed. This method is particularly useful for managing temporary files that should only persist during the session and should be cleaned up afterward, whether they are local or remote files.