2021-01-07 11:46:05 -08:00
# class: Download
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
[Download] objects are dispatched by page via the [`event: Page.download` ] event.
2021-07-19 14:56:28 -07:00
All the downloaded files belonging to the browser context are deleted when the
browser context is closed.
2021-01-07 11:46:05 -08:00
2023-08-10 17:20:39 -07:00
Download event is emitted once the download starts. Download path becomes available once download completes.
2021-01-07 11:46:05 -08:00
```js
2022-11-30 12:36:35 -08:00
// Start waiting for download before clicking. Note no await.
const downloadPromise = page.waitForEvent('download');
await page.getByText('Download file').click();
const download = await downloadPromise;
2023-08-10 17:20:39 -07:00
// Wait for the download process to complete and save the downloaded file somewhere.
await download.saveAs('/path/to/save/at/' + download.suggestedFilename());
2021-01-14 07:48:56 -08:00
```
2021-02-25 22:03:39 -08:00
```java
2023-08-10 17:20:39 -07:00
// Wait for the download to start
2021-02-25 22:03:39 -08:00
Download download = page.waitForDownload(() -> {
2023-08-10 17:20:39 -07:00
// Perform the action that initiates download
page.getByText("Download file").click();
2021-02-25 22:03:39 -08:00
});
2023-08-10 17:20:39 -07:00
// Wait for the download process to complete and save the downloaded file somewhere
download.saveAs(Paths.get("/path/to/save/at/", download.suggestedFilename()));
2021-02-25 22:03:39 -08:00
```
2021-01-14 07:48:56 -08:00
```python async
2023-08-10 17:20:39 -07:00
# Start waiting for the download
2021-01-14 07:48:56 -08:00
async with page.expect_download() as download_info:
2023-08-10 17:20:39 -07:00
# Perform the action that initiates download
2022-10-03 17:02:46 -07:00
await page.get_by_text("Download file").click()
2021-01-14 07:48:56 -08:00
download = await download_info.value
2023-08-10 17:20:39 -07:00
# Wait for the download process to complete and save the downloaded file somewhere
await download.save_as("/path/to/save/at/" + download.suggested_filename)
2021-01-14 07:48:56 -08:00
```
```python sync
2023-08-10 17:20:39 -07:00
# Start waiting for the download
2021-01-14 07:48:56 -08:00
with page.expect_download() as download_info:
2023-08-10 17:20:39 -07:00
# Perform the action that initiates download
2022-10-03 17:02:46 -07:00
page.get_by_text("Download file").click()
2021-01-14 07:48:56 -08:00
download = download_info.value
2023-08-10 17:20:39 -07:00
# Wait for the download process to complete and save the downloaded file somewhere
download.save_as("/path/to/save/at/" + download.suggested_filename)
2021-01-07 11:46:05 -08:00
```
2021-05-13 19:24:15 +02:00
```csharp
2023-08-10 17:20:39 -07:00
// Start the task of waiting for the download before clicking
var waitForDownloadTask = page.WaitForDownloadAsync();
await page.GetByText("Download file").ClickAsync();
var download = await waitForDownloadTask;
// Wait for the download process to complete and save the downloaded file somewhere
await download.SaveAsAsync("/path/to/save/at/" + download.SuggestedFilename);
2021-05-13 19:24:15 +02:00
```
2021-07-06 09:38:50 +02:00
## async method: Download.cancel
2022-07-05 16:24:50 -08:00
* since: v1.13
2021-07-06 09:38:50 +02:00
Cancels a download. Will not fail if the download is already finished or canceled.
Upon successful cancellations, `download.failure()` would resolve to `'canceled'` .
2021-01-07 11:46:05 -08:00
## async method: Download.createReadStream
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-04-19 17:28:57 -03:00
* langs: java, js, csharp
2023-10-17 12:56:56 -07:00
- returns: < [Readable]>
2021-01-07 11:46:05 -08:00
2023-11-13 15:56:50 -08:00
Returns a readable stream for a successful download, or throws for a failed/canceled download.
2021-01-07 11:46:05 -08:00
## async method: Download.delete
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
2021-03-08 19:52:07 -08:00
Deletes the downloaded file. Will wait for the download to finish if necessary.
2021-01-07 11:46:05 -08:00
## async method: Download.failure
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- returns: < [null]|[string]>
2021-03-08 19:52:07 -08:00
Returns download error if any. Will wait for the download to finish if necessary.
2021-01-07 11:46:05 -08:00
2021-05-13 23:18:21 +02:00
## method: Download.page
2022-07-05 16:24:50 -08:00
* since: v1.12
2021-05-13 23:18:21 +02:00
- returns: < [Page]>
Get the page that the download belongs to.
2021-01-07 11:46:05 -08:00
## async method: Download.path
2022-07-05 16:24:50 -08:00
* since: v1.8
2023-10-17 12:56:56 -07:00
- returns: < [path]>
2021-01-07 11:46:05 -08:00
2023-11-13 15:56:50 -08:00
Returns path to the downloaded file for a successful download, or throws for a failed/canceled download. The method will wait for the download to finish if necessary. The method throws when connected remotely.
2021-01-07 11:46:05 -08:00
2021-07-07 10:37:24 -07:00
Note that the download's file name is a random GUID, use [`method: Download.suggestedFilename` ]
to get suggested file name.
2021-01-07 11:46:05 -08:00
## async method: Download.saveAs
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
2021-05-11 22:27:50 +02:00
Copy the download to a user-specified path. It is safe to call this method while the download
is still in progress. Will wait for the download to finish if necessary.
2021-01-07 11:46:05 -08:00
2023-08-10 17:20:39 -07:00
**Usage**
```js
await download.saveAs('/path/to/save/at/' + download.suggestedFilename());
```
```java
download.saveAs(Paths.get("/path/to/save/at/", download.suggestedFilename()));
```
```python async
await download.save_as("/path/to/save/at/" + download.suggested_filename)
```
```python sync
download.save_as("/path/to/save/at/" + download.suggested_filename)
```
```csharp
await download.SaveAsAsync("/path/to/save/at/" + download.SuggestedFilename);
```
2021-01-07 11:46:05 -08:00
### param: Download.saveAs.path
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `path` < [path]>
2021-05-11 22:27:50 +02:00
Path where the download should be copied.
2021-01-07 11:46:05 -08:00
## method: Download.suggestedFilename
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- returns: < [string]>
Returns suggested filename for this download. It is typically computed by the browser from the
[`Content-Disposition` ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition ) response header
or the `download` attribute. See the spec on [whatwg ](https://html.spec.whatwg.org/#downloading-resources ). Different
browsers can use different logic for computing it.
## method: Download.url
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- returns: < [string]>
2021-05-13 19:24:15 +02:00
Returns downloaded url.