Contact Us 1-800-596-4880

file

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:

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

Parameters

Name Description

opts

An object that specifies the following key/value pairs:

  • A unique name (required) for the Content-Disposition header of the part.

  • A path (required) relative to the src/main/resources project path for the Mule app.

  • mime (optional for strings) for the mime type (for example, application/json) to apply to content within the part. This setting cannot be used to transform the input mime type.

  • An optional fileName value for the filename parameter in the part’s Content-Disposition header. Defaults to the string "filename" if not supplied. This value does not need to match the input file name.

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

%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:

<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

{
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

%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

------=_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:

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

Parameters

Name Description

fieldName

A unique name (required) for the Content-Disposition header of the part.

path

A path (required) relative to the src/main/resources project path for the Mule app.

mime

The mime type (optional for strings), for example, application/json, to apply to content within the part. This setting cannot be used to transform the input mime type.

sentFileName

An optional file name value for the filename parameter in the part’s Content-Disposition header. Defaults to the string "filename" if not specified. This value does not need to match the input file name.

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

%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.

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

Output

------=_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--