以下是使用当前API的两个示例。
它使用navigator.webkitPersistentStorage.requestQuota
而不是已弃用的window.webkitStorageInfo.queryUsageAndQuota
:
查询配额
navigator.webkitPersistentStorage.queryUsageAndQuota (
function(usedBytes, grantedBytes) {
console.log('we are using ', usedBytes, ' of ', grantedBytes, 'bytes');
},
function(e) { console.log('Error', e); }
);
请求配额
var requestedBytes = 1024*1024*280;
navigator.webkitPersistentStorage.requestQuota (
requestedBytes, function(grantedBytes) {
console.log('we were granted ', grantedBytes, 'bytes');
}, function(e) { console.log('Error', e); }
);
在这里,我们使用 navigator.webkitPersistentStorage
来查询和请求持久性存储配额。您还可以使用 navigator.webkitTemporaryStorage
来处理临时性存储配额。
当前Chrome实现跟踪该特定规范版本,该版本描述得更详细: https://www.w3.org/TR/quota-api/。
他们还特别解释了临时性
和永久性
之间的区别here:临时数据更像您的tmp
文件夹或弱引用,因为系统可能会随意删除文件,而永久性数据应始终在删除之前通知用户。
您可能希望从包装器开始,以避免使用Web APIs时遇到的所有浏览器兼容性问题(规范的许多部分明确说明:“这是一个提议,可能会在没有任何通知的情况下发生更改”)。例如,Dexie是一个活跃开发的IndexedDb包装器。
chromestore.js是另一个包装器(但多年来没有更新过)。