如何使用导航而不是window.webkitStorageInfo HTML5文件系统API? [英] How to use navigator instead of window.webkitStorageInfo HTML5 File-system API?

查看:449
本文介绍了如何使用导航而不是window.webkitStorageInfo HTML5文件系统API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在这里找到类似的帖子 html-5-filesystem-access-type - 错误。然而,我对结论并不满意,因为我不认为它真的回答了问题 - 给出的解决方案是不推荐使用的代码解决方案。有人知道如何使用 navigator 而不是窗口,因为Chrome控制台正在通知您做什么?



我一直在使用以下工具,但是Chrome控制台不断通知我不这样做,因为它已被弃用。

正在使用的代码

 窗口.webkitStorageInfo.requestQuota(PERSISTENT,1024 * 1024 * 280,function(grantedBytes){
window.webkitRequestFileSystem(PERSISTENT,grantedBytes,onInitFs,errorHandler);
},function(e){
console.log('Error',e);
});

注意: onInitFs errorHandler 都是在其他地方定义的函数,该工作。
$ b 控制台日志 - 我在控制台中得到的消息如下:

 'window.webkitStorageInfo'已弃用。请改用'navigator.webkitTemporaryStorage'
或'navigator.webkitPersistentStorage'。

所以最好的做法是停止使用已弃用的方法。不幸的是,当我用 navigator 替换 window 时,它崩溃(见下文)。如何使用 navigator 而不是 window 访问文件系统?

当前 API的两个示例。



它使用 navigator.webkitPersistentStorage.requestQuota 而不是已弃用的 window.webkitStorageInfo.queryUsageAndQuota



查询限额



  navigator.webkitTemporaryStorage.queryUsageAndQuota(
函数(usedBytes,grantedBytes){
console.log('我们正在使用',usedBytes','','grantedBytes,'bytes');
},
函数(e){console.log('Error',e); }
);



请求配额



  var requestedBytes = 1024 * 1024 * 280; 

navigator.webkitPersistentStorage.requestQuota(
requestedBytes,函数(grantedBytes){
console.log('我们被授予',grantedBytes,'bytes');

},function(e){console.log('Error',e);}
);

您必须选择临时 webkitTemporaryStorage )或持久 webkitPersistentStorage )存储进行查询。

目前的Chrome浏览器实现跟踪这个特定的规格版本,它描述了更多内容:http://www.w3.org/TR/2012/WD-quota-api-20120703/



chromestore.js 为这些数据提供了一个更简单的API。



为了回答你原来的问题,你的新代码应该是这样的:

请求配额并初始化文件系统 h2>

  var requestedBytes = 1024 * 1024 * 280; 

navigator.webkitPersistentStorage.requestQuota(
requestedBytes,function(grantedBytes){
window.webkitRequestFileSystem(PERSISTENT,grantedBytes,onInitFs,errorHandler);

},function(e){console.log('Error',e);}
);


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 the question - the solution given is the deprecated code solution. Does anyone know how to use navigator instead of window as the Chrome console is informing to do?

I have been using the following and it works, but the chrome console keeps informing me not to do so because it is deprecated.

Working Deprecated Code

window.webkitStorageInfo.requestQuota(PERSISTENT, 1024*1024*280, function(grantedBytes) {
    window.webkitRequestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler); 
}, function(e) {
    console.log('Error', e); 
});

Note: onInitFs and errorHandler are both functions defined elsewhere, that work.

Console Log - The message I get in the console is as follows:

'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage'
or 'navigator.webkitPersistentStorage' instead. 

So the best practice would be to stop using the deprecated method. Unfortunately, when I replace window with navigator it crashes (see below). How does one use navigator instead of window to access the file system?

解决方案

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

这篇关于如何使用导航而不是window.webkitStorageInfo HTML5文件系统API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆