How to use navigator instead of window.webkitStorageInfo HTML5 File-system API?

前端 未结 3 1672
無奈伤痛
無奈伤痛 2020-12-08 21:02

So there is a similar post found here html-5-filesystem-access-type-error. However, I\'m not very satisfied with the conclusion because I do not feel it actually answered th

3条回答
  •  暖寄归人
    2020-12-08 21:30

    Below are two examples with the current API.

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

    Query Quota

    navigator.webkitTemporaryStorage.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); }
    );
    

    You have to choose either temporary (webkitTemporaryStorage) or persistent (webkitPersistentStorage) storage to query.

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

    chromestore.js provides an easier API for this data.


    To answer your original question, your new code would look like this:

    Request quota and initialize filesystem

    var requestedBytes = 1024*1024*280; 
    
    navigator.webkitPersistentStorage.requestQuota (
        requestedBytes, function(grantedBytes) {  
            window.webkitRequestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler); 
    
        }, function(e) { console.log('Error', e); }
    );
    

提交回复
热议问题