Skip to main content
Test file upload functionality including images, documents, and other binary data with multipart form submissions.

Loading Files

Use the open() function to read file contents during the init context (before VU execution):

Text Files

Load and parse JSON data:

Binary Files

Add "b" as the second argument to open binary files:
The open() function executes in the init context, so files are loaded once per VU before the test starts. This is more efficient than reading files in every iteration.

Basic File Upload

Upload a file using multipart form data:
When a JavaScript object passed to http.post() contains a FileData property (created by http.file()), k6 automatically constructs a multipart request.

Image Upload Example

Upload an image with metadata:

Advanced Multipart Uploads

For more control over multipart requests, use the FormData polyfill:

Multiple File Upload

When using FormData, you must manually set the Content-Type header with the boundary value.

Why Use FormData?

The FormData polyfill solves two limitations:
  1. Ordered fields: JavaScript objects don’t guarantee property order, but some APIs (like AWS S3) require specific field ordering
  2. Multiple files per field: Upload multiple files under the same form field name
Simple uploads with automatic multipart handling:

Upload with Authentication

Combine file uploads with authentication:

Dynamic File Generation

Generate file content dynamically:

Large File Uploads

Test large file uploads with progress tracking:
Large file uploads consume memory. With multiple VUs, monitor system resources to avoid out-of-memory errors.

Common Scenarios

Best Practices

Load Files Once

Load files in init context, not in the VU function:

Set Appropriate Timeouts

Large uploads may take time:

Validate Upload Results

Always check upload success:

API Reference

Key Functions

  • open(filePath, [mode]) - Load file contents. Use "b" for binary mode
  • http.file(data, [filename], [contentType]) - Wrap data as FileData for multipart requests
  • FormData.append(name, value) - Add fields to multipart form

HTTP Authentication

Add authentication to upload requests

API CRUD Operations

Complete API testing workflows

API Reference: http.file()

Complete http.file() documentation

API Reference: open()

Complete open() documentation