---
schema_version: "1"
id: fa5977c5b9205ec6f21bb21bffed1ed3f265e4a6c9eaed684b1879330d3a11c2
title: file
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-multipart-functions-file.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-multipart-functions-file
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 3fdd84e40062744931a485bb99cc1457fd8314bf559af7c6a614b1da09977987
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# file

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## file(opts: {| name: String, path: String, mime?: String, fileName?: String |})

Creates a `MultipartPart` data structure from a resource file.

This version of the `file` function accepts as argument an object containing key/value pairs,
enabling you to enter the key/value pairs in any order, for example:

```txt
Multipart::file({ name: "myFile", path: "myClients.json", mime: "application/json", fileName: "partMyClients.json"})
```

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><div><div><p><code>opts</code></p></div></div></td><td><div><div><p>An object that specifies the following key/value pairs:</p></div><div><ul><li><p>A unique <code>name</code> (required) for the <code>Content-Disposition</code> header of the part.</p></li><li><p>A <code>path</code> (required) relative to the <code>src/main/resources</code> project path for the Mule app.</p></li><li><p><code>mime</code> (optional for strings) for the mime type (for example, <code>application/json</code>) to apply to content within the part. This setting <em>cannot</em> be used to transform the input mime type.</p></li><li><p>An optional <code>fileName</code> value for the <code>filename</code> parameter in the part’s <code>Content-Disposition</code> header. Defaults to the string <code>"filename"</code> if not supplied. This value does not need to match the input file name.</p></li></ul></div></div></td></tr></tbody></table>

### Example

This example creates a `MultipartPart` from a file accessible to the DataWeave function, the file
name is `orders.xml` and is located in the Mule application’s `/src/main/resources` folder.

The `file` function locates the external `orders.xml` file and uses key/value pairs to indicate the
various parameters needed to build the `MultipartPart`.

- The `name` can be anything, but it usually coincides with the required parameter needed by the
  receiving server that accepts this `Multipart` payload.

- The `path` is set to `./orders.xml`, which is the path and name for the `orders.xml` file that is
  loaded into the `MultipartPart`.

- The `mime` parameter specifies the correct MIME type for the file. In this case, it is
  `application/xml`.

- The `filename` can be changed to any value, it can be different from the actual input file’s
  filename.

Note that the output of this example is not compatible with the `multipart/form-data` output type
because it is just one part of a `Multipart` structure. To create a valid `multipart/form-data`
output, use the `Multipart::form()` function with one or more `Multipart` files and/or fields.

#### Source

```DataWeave
%dw 2.0
import dw::module::Multipart
output application/dw
var ordersFilePath = "./orders.xml"
---
Multipart::file{ name: "file", path: ordersFilePath, mime: "application/xml", fileName: "orders.xml" }
```

#### Input

A file called `orders.xml` located in `src/main/resources` with the following content:

```xml
<orders>
  <order>
    <item>
      <id>1001</id>
      <qty>1</qty>
      <price>\$100</price>
    </item>
    <item>
      <id>2001</id>
      <qty>2</qty>
      <price>\$50</price>
    </item>
  </order>
</orders>
```

#### Output

```json
{
  "headers": {
    "Content-Type": "application/xml",
    "Content-Disposition": {
      "name": "file",
      "filename": "orders.xml"
    }
  },
  "content": "<?xml version='1.0' encoding='UTF-8'?>\n<orders>\n  <order>\n    <item>\n      <id>1001</id>\n      <qty>1</qty>\n      <price>\$100</price>\n    </item>\n    <item>\n      <id>2001</id>\n      <qty>2</qty>\n      <price>\$50</price>\n    </item>\n  </order>\n</orders>"
}
```

### Example

This example inserts file content from a `MultipartPart` into a `Multipart`, resulting in a
`multipart/form-data` output. The example uses the `form` function to create the `Multipart` and
uses `file` to create a part.

The `Multipart::form()` function accepts an array of `Multipart` items, where each part can be
created using the `Multipart::field()` or `Multipart::file()` functions.

#### Source

```DataWeave
%dw 2.0
import dw::module::Multipart
output multipart/form-data
var ordersFilePath = "./orders.xml"
var myArgs = { name: "file", path: ordersFilePath, mime: "application/xml", fileName: "myorders.xml"}
---
Multipart::form([
  Multipart::file(myArgs)
])
```

#### Output

```json
------=_Part_5349_1228640551.1560391284935
Content-Type: application/xml
Content-Disposition: form-data; name="file"; filename="myorders.xml"
<?xml version='1.0' encoding='UTF-8'?>
<orders>
  <order>
    <item>
      <id>1001</id>
      <qty>1</qty>
      <price>$100</price>
    </item>
    <item>
      <id>2001</id>
      <qty>2</qty>
      <price>$50</price>
    </item>
  </order>
</orders>
------=_Part_5349_1228640551.1560391284935--
```

## file(fieldName: String, path: String, mime: String = 'application/octet-stream', sentFileName: String = 'filename')

Creates a `MultipartPart` data structure from a resource file.

This version of the `file` function accepts String arguments in a comma-separated list, for example:

```txt
Multipart::field("myFile", myClients, 'application/json', "partMyClients.json")
```

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>fieldName</code></p></td><td><div><div><p>A unique name (required) for the <code>Content-Disposition</code> header of the part.</p></div></div></td></tr><tr><td><p><code>path</code></p></td><td><div><div><p>A path (required) relative to the <code>src/main/resources</code> project path for the Mule app.</p></div></div></td></tr><tr><td><p><code>mime</code></p></td><td><div><div><p>The mime type (optional for strings), for example, <code>application/json</code>, to apply to content within the part. This setting <em>cannot</em> be used to transform the input mime type.</p></div></div></td></tr><tr><td><p><code>sentFileName</code></p></td><td><div><div><p>An optional file name value for the <code>filename</code> parameter in the part’s <code>Content-Disposition</code> header. Defaults to the string <code>"filename"</code> if not specified. This value does not need to match the input file name.</p></div></div></td></tr></tbody></table>

### Example

This example inserts file content from a `MultipartPart` into a `Multipart` data structure. It uses
the `form` function to create the `Multipart` type and uses `file` to create a part named `myClient`
with JSON content from an external file `myClients.json`. It also specifies `partMyClients.json` as
the value for to the `filename` parameter.

#### Source

```DataWeave
%dw 2.0
import dw::module::Multipart
var myClients = "myClients.json"
output multipart/form-data
---
Multipart::form([
 Multipart::file("myFile", myClients, 'application/json', "partMyClients.json")
])
```

#### Input

A file called `myClients.json` and located in `src/main/resources` with the following content.

```JSON
clients: {
    client: {
      id: 1,
      name: "Mariano"
    },
    client: {
      id: 2,
      name: "Shoki"
    }
  }
```

#### Output

```txt
------=_Part_1586_1887987980.1542569342438
Content-Type: application/json
Content-Disposition: form-data; name="myFile"; filename="partMyClients.json"

{
   clients: {
     client: {
       id: 1,
       name: "Mariano"
     },
     client: {
       id: 2,
       name: "Shoki"
     }
   }
}
------=_Part_1586_1887987980.1542569342438--
```
