Work with the File object

Completed

There are several AL methods that you can use to open files, import and export files to and from Dynamics 365 Business Central, and more.

The file data type provides access to files. Files can be opened in text or binary mode. You can read from or write to a file by creating input and output streams, respectively.

You can use file handling to import or export text-based data files that require parsing to interpret on input or that require special formatting when you are sending output. For example, you can use file handling to import data that is not record-oriented and doesn't have either a fixed width or delimited structure. You can import data from or export data to any external file through file variables.

To access an external file, first declare a variable of type File. This variable is a complex data type and it has many functions that are used to open, read, write, and close external files.

Use each file variable to access one file at a time. If you want to access multiple files at the same time, you must declare one file variable for each file that you access.

Several AL methods are available for you to use to open files, import and export files to and from Business Central, and more. Not all of those functions are useable in the SaaS version of Business Central because users can't access the file system of the cloud server. Conversely, the Business Central server can't access the user's local file system. To be able to work with files, you need to use streams.

For a list of methods, see File Data Type.

Only the UploadIntoStream and DownloadFromStream functions are available for you to use as a developer in the SaaS version of Business Central. The other functions are only available with an on-premises version of Business Central. Those functions need to have access to files instead of streams.

If you ever use the on-premises functions, then we recommend that you observe the following best practices when working with files:

  • Use fully qualified paths to eliminate ambiguity.

  • Be aware of operating system file access restrictions when designing applications that use files. Consider which users have access to files and directories and what access control list (ACL) that you need to apply to file directories.

In addition to the file data type, you can also find useful functions in codeunit 419, File Management, though most of the functions are used in an on-premises environment.

Text encoding is the process of transforming bytes of data into readable characters for users of a system or program. When you import a file as text or as a stream, the text encoding format ensures that all language-specific characters are represented correctly in Business Central. When you export a file as text or as a stream, the text encoding format ensures that all language-specific characters are represented correctly in the system or program that will read the exported file.

Several industry text encoding formats and different systems that support different formats are available. Internally, Business Central uses Unicode encoding. For exporting and importing data with an XMLport, it supports different formats:

  • MS-DOS

  • UTF-8

  • UTF-16

  • ANSI Encoding (Windows encoding)

Data is imported and exported as follows:

  • When data is imported from an external file, it is read by using the format that is specified by the TextEncoding property or parameter, and it is then converted to Unicode in Business Central.

  • When data is exported to an external file, it is converted from Unicode in Business Central and then written to the file in the format that is specified by the TextEncoding property or parameter.

You can set the text encoding to the encoding format that is compatible with the system or program that you'll be exporting to or importing from.

In Business Central online, you have no control over the system locale on Business Central Server. Here, it's recommended to use UTF8/UTF16 for text encodings instead.