Contact Free trial Login

To Read a File Using the File Connector

The File connector can read a file at any point in the flow, unlike in Mule 3, where the transport can only read files as a result of inbound endpoint polling.

Here is the syntax for reading a file:

<file:read path="#[path]"
  outputMimeType="application/csv; headers=false" />

The operation above reads the file in the given path. It returns a MuleMessage with the following attributes:

  • The file’s content as payload.

  • The file’s metadata in the message attributes.

  • If the file does not exist, you receive a FILE:ILLEGAL_PATH error. Note that the operation does not read directories.

MIME Types and Encoding

Each connector tries to determine a file’s MIME type (mimeType) from its extension. However, if first-hand knowledge of the file’s content is necessary, you can force the MIME type to a different value with the outputMimeType parameter.

The same process works for encoding. By default, the connector assumes that the default encoding in the Mule runtime matches the MIME type of the file. However, you can set the encoding through the outputEncoding parameter.

Why MIME Types Matter

DataWeave expressions are embeddable inside operations that generate payloads and other values. Having the correct mimeType set helps DataWeave auto-assign types and also generate the correct outputs. Also, maximizing the use of DataSense’s functionality improves the user experience.

File Locking

You can set a file system lock on the file when it is being read by setting the lock parameter to true (defaults to false). When enabled, this feature asks the operating system to lock the file, which prevents any other process (or Mule flow) from accessing that file while the lock is held. The lock is automatically released when one of the following things occurs:

  • The Mule flow, which locked the file, ends.

  • The file content has been fully read.

Note that if the file is already locked by someone else, the connector will not be able to lock it, and you will receive a FILE:FILE_LOCK error.

Repeatable Streams

The List operation makes use of the repeatable streams functionality introduced in Mule 4. The operation returns a list of messages, where each message represents a file in the list and holds a stream to the file. A stream is repeatable by default.

For more information on this topic, see Streaming in Mule 4.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.