> ## Documentation Index
> Fetch the complete documentation index at: https://intunedhq.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# downloadFile

Downloads a file from a web page using various trigger methods. This function provides three flexible ways to initiate file downloads:

* **URL**: Creates a new page, navigates to the URL, waits for download, then automatically closes the page. Ideal for direct download links.
* **Locator**: Uses the current page to click the element and capture the resulting download. Perfect for download buttons or interactive elements.
* **Callback**: Executes the provided function with the page object and captures the first triggered download. Offers maximum flexibility for complex download scenarios.

```typescript theme={null}
export declare function downloadFile(input: {
  page: Page;
  trigger: Trigger;
  timeoutInMs?: number;
}): Promise<Download>;
```

## Examples

<CodeGroup>
  ```typescript Download from direct URL theme={null}
  import { downloadFile } from "@intuned/browser";
  import { BrowserContext, Page } from "playwright";

  interface Params {}

  export default async function handler(
    params: Params,
    page: Page,
    context: BrowserContext
  ) {
    // Download from a direct URL, this will open the url and automatically download the content in it.
    const download = await downloadFile({
      page,
      trigger:
        "https://intuned-docs-public-images.s3.amazonaws.com/32UP83A_ENG_US.pdf",
    });
    const file_name = download.suggestedFilename();
    return file_name;
  }
  ```

  ```typescript Locator Trigger theme={null}
  import { downloadFile } from "@intuned/browser";
  import { BrowserContext, Page } from "playwright";

  interface Params {}

  export default async function handler(
    params: Params,
    page: Page,
    context: BrowserContext
  ) {
    await page.goto("https://sandbox.intuned.dev/pdfs");
    const download = await downloadFile({
      page,
      trigger: page.locator("xpath=//tbody/tr[1]//*[name()='svg']"),
    });
    const file_name = download.suggestedFilename();
    return file_name;
  }
  ```

  ```typescript Callback Trigger theme={null}
  import { downloadFile } from "@intuned/browser";
  import { BrowserContext, Page } from "playwright";

  interface Params {}

  export default async function handler(
    params: Params,
    page: Page,
    context: BrowserContext
  ) {
    await page.goto("https://sandbox.intuned.dev/pdfs");
    const download = await downloadFile({
      page,
      trigger: async (page) => {
        await page.locator("xpath=//tbody/tr[1]//*[name()='svg']").click();
      },
    });
    const file_name = download.suggestedFilename();
    return file_name;
  }
  ```
</CodeGroup>

## Arguments

<ResponseField name="input" type="Object" required>
  Configuration object for the download operation

  <Expandable title="properties" defaultOpen>
    <ResponseField name="input.page" type="Page" required>
      The Playwright Page object to use for the download
    </ResponseField>

    <ResponseField name="input.trigger" type="Trigger" required>
      The [Trigger](../type-references/Trigger) method to initiate the download
    </ResponseField>

    <ResponseField name="input.timeoutInMs" type="number">
      Maximum time in milliseconds to wait for the download to trigger. Defaults to
      5000\.
    </ResponseField>
  </Expandable>
</ResponseField>

## Returns: `Promise<Download>`

Promise that resolves to a [Playwright Download object](https://playwright.dev/docs/api/class-download)
