Google chrome extension - howto access and store local files for later use
I'm currently writ开发者_StackOverflowing my first google chrome extension. Simplified, I want to add a file browser in the extensions popup, browser an image and insert that image in the currently opened tab.
Additionally I want to store the selected file in the extension for later reuse.
First approach was an , loading of file content with FileAPI and storing the file content base64 encoded in localStorage. Unfortunately that stopped working after a few images due to space limitation of localStorage.
Better approach would be to get the absolute filename of the chosen file and reread the file everytime necessary - unfortunately I was unable to get the complete file including path, just got the filename without path.
Another approach could be WebSQL, but this seems to be complicated - can anybody confirm if or if not we have a quota there as well?
Does anybody made anything like that beforehand? I would be glad to get any help.
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.
精彩评论