*** title: File Uploads description: Upload files to form inputs in browser runtimes. ------------------------------------------------------------- Browser runtimes support uploading files to `` elements. Since the browser runs remotely, files are sent as base64-encoded payloads. ## Setting input files ### Playwright Use `setInputFiles` on a locator or page: ```ts import { readFileSync } from "fs"; // Via locator await runtime.page.locator("input[type=file]").setInputFiles({ name: "report.pdf", mimeType: "application/pdf", buffer: readFileSync("./report.pdf").toString("base64"), }); // Via page selector await runtime.page.setInputFiles("input[type=file]", { name: "report.pdf", mimeType: "application/pdf", buffer: readFileSync("./report.pdf").toString("base64"), }); ``` #### Multiple files ```ts await runtime.page.locator("input[type=file]").setInputFiles([ { name: "page1.png", mimeType: "image/png", buffer: readFileSync("./page1.png").toString("base64"), }, { name: "page2.png", mimeType: "image/png", buffer: readFileSync("./page2.png").toString("base64"), }, ]); ``` #### Clear selected files ```ts await runtime.page.locator("input[type=file]").setInputFiles([]); ``` ### File payload format | Field | Type | Description | | ---------- | -------- | --------------------------- | | `name` | `string` | Filename | | `mimeType` | `string` | MIME type | | `buffer` | `string` | Base64-encoded file content | ### Stagehand Stagehand uses the same file payload format: ```ts await runtime.page.locator("input[type=file]").setInputFiles({ name: "data.csv", mimeType: "text/csv", buffer: readFileSync("./data.csv").toString("base64"), }); ``` ## Multipart form data (Playwright) For API requests that accept file uploads via multipart form data: ```ts await runtime.page.request.post("https://api.example.com/upload", { multipart: { name: "My Report", file: { name: "report.pdf", mimeType: "application/pdf", buffer: readFileSync("./report.pdf").toString("base64"), }, }, }); ``` ## Example: upload a document ```ts import { readFileSync } from "fs"; await runtime.page.goto("https://app.example.com/upload"); // Set the file on the input await runtime.page.locator("#file-input").setInputFiles({ name: "invoice.pdf", mimeType: "application/pdf", buffer: readFileSync("./invoice.pdf").toString("base64"), }); // Submit the form await runtime.page.locator("#upload-btn").click(); // Wait for upload confirmation await runtime.page.locator(".upload-success").waitFor(); ```