Skip to main content
The Cloudflare SDK supports multiple file upload methods to accommodate different runtime environments and use cases.

Supported file types

Request parameters that accept file uploads support multiple formats:

File API

Web-standard File objects or compatible structures

ReadStream

Node.js fs.ReadStream for file system access

Response

fetch Response objects or compatible structures

toFile Helper

SDK’s toFile() helper for buffers and arrays

File upload methods

Use fs.createReadStream() for optimal performance in Node.js:
This is the recommended approach for Node.js environments as it streams the file without loading it entirely into memory.

The toFile helper

The toFile() helper converts raw data into an uploadable format:

Usage examples

Buffer | Uint8Array | ArrayBuffer
required
Raw binary data to upload
string
required
Name for the uploaded file
string
MIME type of the file (e.g., "application/json")

Reading files from disk

Use fileFromPath() to read files from the filesystem:
fileFromPath() is only available in Node.js environments.

Complete upload examples

API Gateway schema upload

Dynamic file generation

Remote file upload

Multipart form data

File uploads use multipart/form-data encoding automatically:
The SDK automatically sets the correct Content-Type: multipart/form-data header and constructs the multipart body.

File metadata

When using the File API or toFile(), you can specify metadata:

Best practices

1

Use streams for large files

In Node.js, prefer fs.createReadStream() to avoid loading entire files into memory:
2

Set correct MIME types

Specify the correct content type for better compatibility:
3

Handle file errors

Wrap file operations in try-catch blocks:
4

Validate files before upload

Check file size and format before initiating uploads:

TypeScript types

The SDK provides type definitions for uploadable values:
Any object matching this structure can be used for file uploads.