3

我目前正在编写我的第一个 google chrome 扩展程序。简化后,我想在扩展弹出窗口中添加文件浏览器,浏览图像并将该图像插入当前打开的选项卡中。

此外,我想将所选文件存储在扩展名中以供以后重用。

第一种方法是使用 FileAPI 加载文件内容并将文件内容 base64 编码存储在 localStorage 中。不幸的是,由于 localStorage 的空间限制,在几张图像后停止工作。

更好的方法是获取所选文件的绝对文件名并在每次必要时重新读取文件 - 不幸的是,我无法获取包含路径的完整文件,只是得到了没有路径的文件名。

另一种方法可能是 WebSQL,但这似乎很复杂 - 任何人都可以确认我们是否也有配额吗?

有没有人事先做过这样的事情?我很乐意得到任何帮助。

4

1 回答 1

1

Depending on what you need to do with the cached files, you could simply use the FileIO API to save the binary to your extension's sandbox environment. Just storing the filepath to the original file probably won't work as the API can only read within it's sandbox [unless your choosing the files yourself through an input].

If necessary, you can add the "unlimitedStorage" Permission (more at the developer documentation )to your manifest.json in order to override the storage limitations.

"permissions": [
  "unlimitedStorage"
],

This will only apply to the File I/O (temporary/persistent) and Web SQL (temporary only). Due to it's synchronous implementation, localStorage has a maximum of ~ 2700000 characters (as it is stored as UTF-16), which won't be increased by setting this permission.

With the File API and Chrome 13+, you can request a certain amount of quota (see here and here for full example). This has been introduced to work for Web apps and works without setting the unlimitedStorage permission. (However, if the permission is set, the user is currently not asked to allow the storage request)

webkitStorageInfo.requestQuota( 
    webkitStorageInfo.PERSISTENT
    newQuotaInBytes,
    quotaCallback,
    errorCallback);

In my experiments, this seems to be the only way to persistently store large amounts of data.

于 2011-11-23T20:46:07.633 回答