# File handling

Through CDK, You can build operations that can handle file uploads and downloads.

You can send `binary` data or `multipart/form-data` depending on the service you are using.

You can find code samples for below on how to do this in both:

> **Look out for the \`FileReference\` type used in the samples that follow.** It's recommended to use the `FileReference` type for handling files. This type is specifically designed to streamline file operations in the Tray builder, ensuring consistency and reliability in your file handling logic.You don't need to define this type yourself. Simply import it from the CDK-DSL.`import { FileReference } from "@trayio/cdk-dsl/connector/operation/OperationHandler";`

## Binary implementation

***

### File upload

#### handler.ts

```typescript
handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
  http
    .post("https://someapi.com/someresource")
    .handleRequest((ctx, input, request) => request.withBodyAsFile(input.file))
    .handleResponse((ctx, input, response) => response.parseWithBodyAsJson())
);
```

#### input.ts

```js
import { FileReference } from "@trayio/cdk-dsl/connector/operation/OperationHandler";

export type UploadFileInput = \{
  file: FileReference,
\};
```

#### output.ts

```js
import { FileReference } from "@trayio/cdk-dsl/connector/operation/OperationHandler";

export type UploadFileInput = \{
  file: FileReference,
\};
```

### File download

#### handler.ts

```js
handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
  http
    .get("https://someapi.com/someresource")
    .handleRequest((ctx, input, request) => request.withoutBody())
    .handleResponse(
      (ctx, input, response) =>
        response.parseWithBodyAsFile <
        DownloadFileOutput["file"] >
        ((file) => OperationHandlerResult.success({ file }))
    )
);
```

#### input.ts

```js
export type DownloadFileInput = {}; // Add input parameters as needed.
```

#### output.ts

```js
import { FileReference } from "@trayio/cdk-dsl/connector/operation/OperationHandler";

export type DownloadFileOutput = \{
  file: FileReference,
\};
```

## Multipart form data implementation

***

### File upload

#### handler.ts

```js
export const myOperationHandler = OperationHandlerSetup.configureHandler<
  AuthType,
  InputType,
  OutputType
>((handler) =>
  handler
    .addInputValidation(...Pass your callback for input validation...) //optional
    .addOutputValidation(...Pass your callback for output validation...) //optional
    .withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
      http
        .post("https://someapi.com/someresource")
        .handleRequest((ctx, input, request) =>
          request.withBodyAsMultipart({
            fields: {},
            files: \{
              // where `files` is an array of FileReference.
              file1: input.files[0],
              file2: input.files[1],
            \},
          }),
        )
        .handleResponse((ctx, input, response) =>
          response.parseWithBodyAsJson(),
        ),
    ),
);
```

#### input.ts

```js
import { FileReference } from "@trayio/cdk-dsl/connector/operation/OperationHandler";

export type UploadFileInput = \{
  files: FileReference[],
\};
```

#### output.ts

```js
export type UploadFileOutput = {}; // Add fields as per response from the API.
```
