What are the details can be obtained from webkitStorageInfo.queryUsageAndQuota()

后端 未结 3 1648
悲哀的现实
悲哀的现实 2021-01-20 19:28

webkitStorageInfo.queryUsageAndQuota() is used to find out the usage stats of the files that have been stored in the file system using the HTML5 file system API I suppose. C

相关标签:
3条回答
  • 2021-01-20 19:42

    Below are two examples with the current API.

    It uses navigator.webkitPersistentStorage.requestQuota instead of the deprecated window.webkitStorageInfo.queryUsageAndQuota:

    Query Quota

    navigator.webkitPersistentStorage.queryUsageAndQuota ( 
        function(usedBytes, grantedBytes) {  
            console.log('we are using ', usedBytes, ' of ', grantedBytes, 'bytes');
        }, 
        function(e) { console.log('Error', e);  }
    );
    

    Request Quota

    var requestedBytes = 1024*1024*280; 
    
    navigator.webkitPersistentStorage.requestQuota (
        requestedBytes, function(grantedBytes) {  
            console.log('we were granted ', grantedBytes, 'bytes');
    
        }, function(e) { console.log('Error', e); }
    );
    

    Here we use navigator.webkitPersistentStorage to query and request persistent storage quota. You can also use navigator.webkitTemporaryStorage to work with temporary storage quota.

    The current Chrome implementation tracks this specific spec version, which describes things a bit more: https://www.w3.org/TR/quota-api/.

    They also specifically explain the difference between temporary and permanent here: Temporary data is more like your tmp folder or a weak reference, in that, things might get deleted on the whim of the system, while permanent data should always inform the user before getting deleted.

    You might want to start with a wrapper to escape all the browser compatibility hell that comes with working against Web APIs (many parts of the specs explicitly state: "This is a proposal and may change without any notices"). Dexie, for example, is an actively developed wrapper for IndexedDb.

    chromestore.js is another wrapper (but has not been touched in years).

    0 讨论(0)
  • 2021-01-20 19:48

    Replace function(){...} with console.log.bind(console), and you will find out.

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

    The explanation of the callback is found 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); 
    };

    So, the first number indicates how many bytes are used,
    the second number shows the quota's size.

    0 讨论(0)
  • 2021-01-20 20:05
    // 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); 
    } );
    

    Where used and remaining are in bytes

    Taken from google devlopers

    0 讨论(0)
提交回复
热议问题