有什么细节可以从webkitStorageInfo.queryUsageAndQuota()获得

时间:2012-05-07 06:14:04

标签: html5 google-chrome google-chrome-extension fileapi html5-filesystem

webkitStorageInfo.queryUsageAndQuota()用于查找使用我认为的HTML5文件系统API存储在文件系统中的文件的使用情况统计信息。任何人都可以向我提供可以在提供给此函数的回调中获得的详细信息。

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

3 个答案:

答案 0 :(得分:12)

以下是当前 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/

他们还具体解释了temporarypermanent here之间的区别:临时数据更像是您的tmp文件夹或弱引用,因为,事情可能会得到根据系统的想法删除,而永久数据应该在被删除之前通知用户。

您可能希望从包装器开始,以逃避与Web API相关的所有浏览器兼容性问题(规范的许多部分明确指出:"这是一个提案,可能会在没有任何通知的情况下进行更改&#34 )。例如,Dexie是一个积极开发的IndexedDb包装器。

chromestore.js是另一个包装器(但多年来没有被触及)。

答案 1 :(得分:4)

function(){...}替换为console.log.bind(console),您会发现。

> window.webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.PERSISTENT, console.log.bind(console))
undefined  // Return value of ^
0 0        // Printed results, argument 0 and argument 1

找回回调的解释here

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); 
};

因此,第一个数字表示使用了多少字节,
第二个数字显示配额的大小。

答案 2 :(得分: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); 
} );

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

取自google devlopers