page.on('download') event.
Overview
Methods
url()
Get the download URL. Returns:string
suggestedFilename()
Get the suggested filename from the server. Returns:string
This is the filename suggested by the Content-Disposition header or derived from the URL:
path()
Get the path to the downloaded file. Returns:Promise<string>
Waits for the download to finish and returns the path to the downloaded file:
saveAs(path)
Save the download to a specific path.string
required
Path where the file should be saved
Promise<void>
failure()
Get the failure reason if the download failed. Returns:Promise<string | null>
Returns null if the download was successful:
createReadStream()
Get a readable stream for the downloaded file. Returns:Promise<Readable>
Useful for processing large files without loading them entirely into memory:
cancel()
Cancel the download. Returns:Promise<void>
delete()
Delete the downloaded file. Returns:Promise<void>
page()
Get the page that initiated the download. Returns:Page
Examples
Basic Download
Check Download Success
Save with Suggested Filename
Stream Large File
Handle Multiple Downloads
Cancel Download
Verify Download Content
Download from Link Click
Wait for Download with Timeout
Best Practices
Wait Before Clicking
Set up the download listener before triggering the download:Use Promise.all for Simultaneous Actions
Clean Up Downloads
Delete temporary downloads after testing:Handle Download Failures
Common Issues
Download Not Triggered
Ensure the download actually starts:Popup Downloads
Handle downloads from popups:Related
- Page - Page object
- File Uploads - Handling file uploads
