Contact Us 1-800-596-4880

file

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

Creates a MultipartPart data structure from a resource file.

This version of the file function accepts arguments as an array of objects that use the parameter names as keys, for example:

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

Parameters

Name Description

opts

Array of objects that specifies:

  • 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 inserts file content from a MultipartPart into a Multipart data structure. It uses the form function to create the Multipart 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
output multipart/form-data
var myClients = "myClients.json"
var myArgs = { name: "myFile", path: "myClients.json", mime: "application/json", * fileName: "partMyClients.json"}
---
Multipart::form([
  Multipart::file(myArgs)
])

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

file(String, String, String, String)

Creates a MultipartPart data structure from a resource file.

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

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

Parameters

Name Description

opts

Array of objects that specifies:

  • 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 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--