webkitStorageInfo.queryUsageAndQuota()可以获得哪些细节信息?

3

webkitStorageInfo.queryUsageAndQuota()用于查询使用HTML5文件系统API存储在文件系统中的文件的使用情况统计信息。请问有人能提供回调函数中可以获得的详细信息吗?

window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.PERSISTENT, function() {
   //what all details can be obtained in this function as its arguments?
})
3个回答

11

以下是使用当前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是另一个包装器(但多年来没有更新过)。


2
推荐使用https://github.com/ebidel/filer.js,它是在Filesystem API之上的另一个辅助工具,并使用了更新的配额API。 - ebidel

5
console.log.bind(console)替代function(){...},你就会发现。
> window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.PERSISTENT, console.log.bind(console))
undefined  // Return value of ^
0 0        // Printed results, argument 0 and argument 1

回调函数的解释在这里找到:

interface StorageInfo { 
  ....
  // Queries the current quota and how much data is stored for the host. 
  void queryUsageAndQuota( 
      unsigned short storageType, 
      optional StorageInfoUsageCallback successCallback, 
      optional StorageInfoErrorCallback errorCallback); 
  ...
[NoInterfaceObject, Callback=FunctionOnly] 
interface StorageInfoUsageCallback { 
  void handleEvent(unsigned long long currentUsageInBytes, 
                   unsigned long long currentQuotaInBytes); 
};

这段代码表示了一个回调函数,用于获取存储信息。第一个数字表示已使用的字节数,第二个数字表示配额的大小。


谢谢Rob。还有一个澄清,显示的大小是以字节为单位的吧? - Jophin Joseph

2
// Request storage usage and capacity left
window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY, //the type can be either TEMPORARY or PERSISTENT
function(used, remaining) {
  console.log("Used quota: " + used + ", remaining quota: " + remaining);
}, function(e) {
  console.log('Error', e); 
} );

使用和剩余的值以字节为单位

来自谷歌开发者文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接