Chrome packaged application - downloading file from Webview

▼魔方 西西 提交于 2019-12-02 04:33:24

File downloads from the webview guest are gated by a permissionrequest event per download attempt. The default response is to deny download permission.

Note: The download permissionrequest will not land until Chrome 30 now, and it appears that documentation has not yet been made available. It is generally a stable API though, and it is unlikely to change between now and then.

In order to override this behavior, you need to handle the event and explicitly allow the download to happen. As an example:

var webview = document.querySelector('#app-webview');
webview.addEventListener('permissionrequest', function(e) {
  if (e.permission === 'download') {
    e.request.allow();
  }
});

The event includes additional information (such as the download URL in e.url) in case you want to further filter your grants.

Be aware that this will only permit the download to happen using the regular Chrome file download experience, which isn't necessarily what you'll want from within a packaged app. Your options are limited until the chrome.downloads API is made available to apps.

One possibility is to deny the download request, grab the URL from the event, and manually manage the download process with XHR, the fileSystem API, and whatever UX you want to build.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!