2021-01-07 11:46:05 -08:00
|
|
|
# class: Download
|
|
|
|
|
|
|
|
[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
|
|
|
|
|
|
|
Download event is emitted once the download starts. Download path becomes available once download completes:
|
|
|
|
|
|
|
|
```js
|
2022-01-13 10:38:22 -08:00
|
|
|
// Note that Promise.all prevents a race condition
|
|
|
|
// between clicking and waiting for the download.
|
2021-01-07 11:46:05 -08:00
|
|
|
const [ download ] = await Promise.all([
|
2022-01-13 10:38:22 -08:00
|
|
|
// It is important to call waitForEvent before click to set up waiting.
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
// Triggers the download.
|
|
|
|
page.locator('text=Download file').click(),
|
2021-01-07 11:46:05 -08:00
|
|
|
]);
|
|
|
|
// wait for download to complete
|
|
|
|
const path = await download.path();
|
2021-01-14 07:48:56 -08:00
|
|
|
```
|
|
|
|
|
2021-02-25 22:03:39 -08:00
|
|
|
```java
|
|
|
|
// wait for download to start
|
2021-03-31 10:38:05 -07:00
|
|
|
Download download = page.waitForDownload(() -> page.click("a"));
|
2021-02-25 22:03:39 -08:00
|
|
|
// wait for download to complete
|
|
|
|
Path path = download.path();
|
|
|
|
```
|
|
|
|
|
|
|
|
```java
|
|
|
|
// wait for download to start
|
|
|
|
Download download = page.waitForDownload(() -> {
|
|
|
|
page.click("a");
|
|
|
|
});
|
|
|
|
// wait for download to complete
|
|
|
|
Path path = download.path();
|
|
|
|
```
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
```python async
|
|
|
|
async with page.expect_download() as download_info:
|
|
|
|
await page.click("a")
|
|
|
|
download = await download_info.value
|
|
|
|
# waits for download to complete
|
|
|
|
path = await download.path()
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
with page.expect_download() as download_info:
|
|
|
|
page.click("a")
|
|
|
|
download = download_info.value
|
|
|
|
# wait for download to complete
|
|
|
|
path = download.path()
|
2021-01-07 11:46:05 -08:00
|
|
|
```
|
|
|
|
|
2021-05-13 19:24:15 +02:00
|
|
|
```csharp
|
2021-05-26 15:11:31 -07:00
|
|
|
var download = await page.RunAndWaitForDownloadAsync(async () =>
|
2021-05-19 17:19:25 -07:00
|
|
|
{
|
|
|
|
await page.ClickAsync("#downloadButton");
|
|
|
|
});
|
2021-05-15 14:02:07 -07:00
|
|
|
Console.WriteLine(await download.PathAsync());
|
2021-05-13 19:24:15 +02:00
|
|
|
```
|
|
|
|
|
2021-07-06 09:38:50 +02:00
|
|
|
## async method: Download.cancel
|
|
|
|
|
|
|
|
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
|
2021-04-19 17:28:57 -03:00
|
|
|
* langs: java, js, csharp
|
2021-01-07 11:46:05 -08:00
|
|
|
- returns: <[null]|[Readable]>
|
|
|
|
|
|
|
|
Returns readable stream for current download or `null` if download failed.
|
|
|
|
|
|
|
|
## async method: Download.delete
|
|
|
|
|
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
|
|
|
|
- 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
|
|
|
|
- returns: <[Page]>
|
|
|
|
|
|
|
|
Get the page that the download belongs to.
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## async method: Download.path
|
2021-01-12 12:14:27 -08:00
|
|
|
- returns: <[null]|[path]>
|
2021-01-07 11:46:05 -08:00
|
|
|
|
2021-03-08 19:52:07 -08:00
|
|
|
Returns path to the downloaded file in case of successful download. The method will
|
2021-04-02 09:47:14 +08:00
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
### param: Download.saveAs.path
|
|
|
|
- `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
|
|
|
|
- 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
|
|
|
|
- returns: <[string]>
|
|
|
|
|
2021-05-13 19:24:15 +02:00
|
|
|
Returns downloaded url.
|