我想将无限数量的文件保存到用户的硬盘上,而不需要用户点击对话框。
我看到的关于使用无限存储的唯一文档是:https://developers.google.com/chrome/whitepapers/storage ,表示它仅适用于 Chrome 应用。
我看到的所有关于 Chrome fileSystem
API 的例子都表明,为了创建一个新文件并写入它,用户需要从 chrome 获取一个条目对象.fileSystem.chooseEntry
函数,这意味着用户需要通过一个对话框。
常规 fileSystem
API 看起来可以将文件保存到用户的扩展沙箱中,而无需让用户通过对话框,但我想知道,它是否具有 "unlimitedStorage"
仅授予应用程序的权限?如果只有 Chrome API 具有该权限,我不会感到震惊。
如果我使用常规的 fileSystem
API,我需要使用 window.webkitRequestFileSystem
请求文件系统(我有点惊讶 window.requestFileSystem
在 Chrome 中对我不起作用),但该方法采用 type
(“persistent”
或 “temporary”
)和 size
(应用程序存储所需的字节数)参数。
您显然不会在 Chrome API 中这样做。但是,普通的 fileSystem
API 允许您在需要时使用配额管理 API 请求更多存储空间,这也记录在我上面链接的页面上
然后 https://developers.google.com/chrome/apps/docs/developers_guide#manifest ,它说:
The
"permissions"
field lets you specify HTML5 permissions that the app requires. By specifying"unlimitedStorage"
and"notifications"
, this app is able to use those HTML5 features without having to repeatedly ask the user for permission. During app installation, the user is told which permissions the app needs; installing the app implicitly grants those permissions for all pages whose URLs match those in the"apps"
field of the manifest.
所以我希望 "unlimitedStorage"
权限只是意味着我可以调用 window.webkitRequestFileSystem(window.PERSISTENT, anySize, successCallback)
,而不是在需要时要求用户提高配额,Chrome 会自动执行。
问题 1:有谁知道这是否是它的工作方式?
但是,如果只有 chrome API 具有 "unlimitedStorage"
权限,我可以尝试使用常规 API 创建一个文件条目,并将该条目传递给 Chrome API 函数,因此跳过需要一个对话框,并获得对 "unlimitedStorage"
权限的访问权限。
问题 2: 我可以在一个 API 中创建条目并将其传递给另一个吗?恐怕不同的 API 可能有不同的沙箱。
关于 fileSystem
API 的文档不是很好,所以我想我会问问是否有人从以前的经验中知道。兔子洞越少越好。
最佳答案
首先,为了避免混淆,这里所说的“打包应用”应称为“Chrome应用”。
如果该 blob 将被任何其他应用程序使用,您必须通过 chrome.fileSystem.chooseEntry
获取一个 FileEntry
,以便将数据写入计算机的文件用户可以访问它的系统。 (您可以通过这种方式从用户那里获得一个目录并保存保留的条目,并使用它而无需每次都要求用户做出选择。)
如果 blob 仅供该 Chrome 应用程序使用,您可以使用文件 API 而无需调用 chrome.fileSystem.chooseEntry
。
至于限制,您是正确的,unlimitedStorage
允许无限存储。在我的实验中,尽管有文档,但我从未能够让 Chrome 应用程序要求用户授权特定数量。我用那个权限,好像没有限制。我读过的关于此的 Google 文档似乎不准确。
如果您正在写入外部文件系统,在您调用 chrome.fileSystem.chooseEntry
之后,没有任何限制,您不需要 unlimitedStorage
许可。
关于javascript - Chrome 应用程序中的无限文件存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196265/