下载
介绍
🌐 Introduction
每当页面下载一个附件时,会触发 Page.Download 事件。所有这些附件都会被下载到一个临时文件夹中。你可以使用事件中的 Download 对象获取下载 URL、文件名和负载流。
🌐 For every attachment downloaded by the page, Page.Download event is emitted. All these attachments are downloaded into a temporary folder. You can obtain the download url, file name and payload stream using the Download object from the event.
你可以使用 BrowserType.LaunchAsync() 中的 DownloadsPath 选项来指定下载文件的保存位置。
🌐 You can specify where to persist downloaded files using the DownloadsPath option in BrowserType.LaunchAsync().
当生成这些文件的浏览器上下文关闭时,下载的文件会被删除。
🌐 Downloaded files are deleted when the browser context that produced them is closed.
这是处理文件下载的最简单方法:
🌐 Here is the simplest way to handle the file download:
// 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);
变化
🌐 Variations
如果你不知道是什么启动了下载,你仍然可以处理该事件:
🌐 If you have no idea what initiates the download, you can still handle the event:
page.Download += (sender, download) => Console.WriteLine(download.Url);
请注意,处理事件会分叉控制流,使脚本更难理解。由于主控制流并未等待此操作完成,你的场景可能在文件下载过程中就已经结束。
🌐 Note that handling the event forks the control flow and makes the script harder to follow. Your scenario might end while you are downloading a file since your main control flow is not awaiting for this operation to resolve.
有关上传文件,请参阅上传文件部分。 :::
🌐 For uploading files, see the uploading files section.