Skip to main content
Version: 24.38.0

FileChooser 类

🌐 FileChooser class

文件选择器可让你对请求文件的页面做出反应。

🌐 File choosers let you react to the page requesting for a file.

语法

🌐 Signature

export declare class FileChooser

附注

🌐 Remarks

FileChooser 实例通过 Page.waitForFileChooser() 方法返回。

在浏览器中,一次只能打开一个文件选择器。所有文件选择器必须被接受或取消。如果不这样做,将阻止后续的文件选择器出现。

🌐 In browsers, only one file chooser can be opened at a time. All file choosers must be accepted or canceled. Not doing so will prevent subsequent file choosers from appearing.

这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 FileChooser 类的子类。

🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the FileChooser class.

示例

🌐 Example

const [fileChooser] = await Promise.all([
page.waitForFileChooser(),
page.click('#upload-file-button'), // some button that triggers file selection
]);
await fileChooser.accept(['/tmp/myfile.pdf']);

方法

🌐 Methods

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

accept(paths)

接受带有给定文件路径的文件选择器请求。

🌐 Accept the file chooser request with the given file paths.

附注

这不会验证文件路径是否存在。此外,如果路径是相对的,则它是相对于当前工作目录解析的。对于连接到远程 Chrome 环境的本地脚本,路径必须是绝对路径。

🌐 This will not validate whether the file paths exists. Also, if a path is relative, then it is resolved against the current working directory. For locals script connecting to remote chrome environments, paths must be absolute.

cancel()

关闭文件选择器而不选择任何文件。

🌐 Closes the file chooser without selecting any files.

isMultiple()

文件选择器是否允许文件选择。

🌐 Whether file chooser allow for multiple file selection.