Skip to main content

Download

[下载] 对象通过 Page.Download 事件按页面分发。

当浏览器上下文关闭时,属于浏览器上下文的所有下载文件都将被删除。

🌐 All the downloaded files belonging to the browser context are deleted when the browser context is closed.

一旦下载开始,会触发下载事件。下载完成后,下载路径将可用。

🌐 Download event is emitted once the download starts. Download path becomes available once download completes.

// 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);

方法

🌐 Methods

CancelAsync

Added in: v1.13 download.CancelAsync

取消下载。如果下载已经完成或已被取消,将不会失败。取消成功后,download.failure() 将解析为 'canceled'

🌐 Cancels a download. Will not fail if the download is already finished or canceled. Upon successful cancellations, download.failure() would resolve to 'canceled'.

用法

await Download.CancelAsync();

返回


CreateReadStreamAsync

Added before v1.9 download.CreateReadStreamAsync

成功下载时返回可读流,下载失败/取消时抛出异常。

🌐 Returns a readable stream for a successful download, or throws for a failed/canceled download.

note

如果你不需要可读流,通常在下载完成后直接从磁盘读取文件会更简单。参见 Download.PathAsync()

🌐 If you don't need a readable stream, it's usually simpler to read the file from disk after the download completed. See Download.PathAsync().

用法

await Download.CreateReadStreamAsync();

返回

  • [Stream]#

DeleteAsync

Added before v1.9 download.DeleteAsync

删除已下载的文件。如果有必要,会等待下载完成。

🌐 Deletes the downloaded file. Will wait for the download to finish if necessary.

用法

await Download.DeleteAsync();

返回


FailureAsync

Added before v1.9 download.FailureAsync

如果有任何下载错误,将返回错误。如有必要,会等待下载完成。

🌐 Returns download error if any. Will wait for the download to finish if necessary.

用法

await Download.FailureAsync();

返回


Page

Added in: v1.12 download.Page

获取下载所属的页面。

🌐 Get the page that the download belongs to.

用法

Download.Page

返回


PathAsync

Added before v1.9 download.PathAsync

返回已成功下载文件的路径,或在下载失败/取消时抛出异常。如果必要,该方法会等待下载完成。远程连接时,该方法会抛出异常。

🌐 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.

请注意,下载的文件名是一个随机的 GUID,可以使用 Download.SuggestedFilename 获取建议的文件名。

🌐 Note that the download's file name is a random GUID, use Download.SuggestedFilename to get suggested file name.

用法

await Download.PathAsync();

返回


SaveAsAsync

Added before v1.9 download.SaveAsAsync

将下载内容复制到用户指定的路径。在下载仍在进行时调用此方法是安全的。如有必要,将等待下载完成。

🌐 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.

用法

await download.SaveAsAsync("/path/to/save/at/" + download.SuggestedFilename);

参数

  • path string#

    应复制下载的路径。

返回


SuggestedFilename

Added before v1.9 download.SuggestedFilename

返回此下载的建议文件名。通常由浏览器根据 Content-Disposition 响应头或 download 属性计算得出。有关详细规范,请参见 whatwg。不同的浏览器可能使用不同的逻辑来计算它。

🌐 Returns suggested filename for this download. It is typically computed by the browser from the Content-Disposition response header or the download attribute. See the spec on whatwg. Different browsers can use different logic for computing it.

用法

Download.SuggestedFilename

返回


Url

Added before v1.9 download.Url

返回下载的 url。

🌐 Returns downloaded url.

用法

Download.Url

返回