大网数据广告活动
Web 标准与前端开发

file_handlers

· 52CE 我爱测

The `file_handlers` member specifies an array of objects representing the types of files an installed progressive web app (PWA) can handle.

The file_handlers member specifies an array of objects representing the types of files an installed progressive web app (PWA) can handle.

The file_handlers member is read by the browser when the PWA is installed and used to associate the application with a given set of file types at the operating system level.

For example, a PWA can be registered to handle files that match the text/plain MIME type. Once this PWA is installed, the operating system is able to use it to handle text files, opening the PWA when the user opens a file of this type. Note that other applications may also be registered as text file handlers, and the way operating systems manage the association between file types and applications, and the way they let users choose an application to handle a given file can vary from one device to another.

[!NOTE] While the file_handlers member is used to register a PWA with a given set of file types, this only results in the operating system launching the PWA when a matching file is opened. The PWA then needs to actually handle the file using JavaScript code. See Handling the files for more information.

Values

An array of objects. Each object in the array must contain the following values (action and accept are required):

  • action

    • : A string containing the URL to navigate to when a file is handled. This URL must be within the navigation scope of the PWA, which is the set of URLs that the PWA can navigate to. The navigation scope of a PWA defaults to its start_url member, but can also be defined by using the scope member.
  • accept

    • : An object. For each property in the object:
      • The property key is a MIME type.
      • The property value is an array of strings representing file extensions associated with that MIME type.

Examples

In this example, a web app manifest declares one file handler that registers the app to handle audio files. Other manifest members such as name, or icons are not included in this example for brevity:

{
  "file_handlers": [
    {
      "action": "/handle-audio-file",
      "accept": {
        "audio/wav": [".wav"],
        "audio/x-wav": [".wav"],
        "audio/mpeg": [".mp3"],
        "audio/mp4": [".mp4"],
        "audio/aac": [".adts"],
        "audio/ogg": [".ogg"],
        "application/ogg": [".ogg"],
        "audio/webm": [".webm"],
        "audio/flac": [".flac"],
        "audio/mid": [".rmi", ".mid"]
      }
    }
  ]
}

Handling the files

To actually implement file handling in a PWA, web developers also need to use window.launchQueue to handle the incoming files in their application JavaScript code.

Handling files is done in the application code that runs on the main thread, not in the application's service worker.

In the following example, LaunchQueue.setConsumer is used to specify a callback function that receives incoming audio files and plays the first one using an HTMLAudioElement.Audio element:

async function playSong(handledFile) {
  const blob = await handledFile.getFile();
  const url = window.URL.createObjectURL(blob);
  const audio = new Audio(url);
  audio.play();
}

if ("launchQueue" in window) {
  window.launchQueue.setConsumer((launchParams) => {
    if (launchParams.files && launchParams.files.length) {
      playSong(launchParams.files[0]);
    }
  });
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。