您当前的位置: 首页 > 学无止境 > 网站建设 网站首页网站建设
html5文件api(2)
发布时间:2017-04-15 19:16:10编辑:雪饮阅读()
window.requestFileSystem:浏览器沙箱保护的本地文件系统
该函数拥有4个参数:
type:文件类型
size:文件大小
successCallback:请求成功的回调
errorCallback:请求失败的回调
demo:(要运行在环境中)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.requestFileSystem=window.requestFileSystem || window.webkitRequestFileSystem;
var fs=null;
var msg="";
if(window.requestFileSystem){
initFs();
}
function initFs(){
//window.TEMPORARY:临时的
window.requestFileSystem(window.TEMPORARY,1024*10,function(filesystem){
fs=filesystem;
document.getElementById("result").innerHTML="当前浏览器可以操作";
},erroHandler);
}
function erroHandler(e){
switch(e.code){
case FileError.NOT_FOUND_ERR:
msg="未找到文件或目录";
break;
case FileError.SECURITY_ERR:
msg="操作不当引起的安全性错误";
break;
}
document.getElementById("result").innerHTML="当前引发的错误:"+msg;
}
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
申请磁盘配额:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
function getSize(){
var size=document.getElementById("capacity").value;
window.webkitStorageInfo.requestQuota(PERSISTENT,size,function(grantdBytes){
var text="申请磁盘成功";
var intBytes;
if(grantdBytes>=1024*1024*1024){
intBytes=Math.floor(grantdBytes/(1024*1024*1024));
text+=intBytes+"GB";
}
if(grantdBytes>=1024*1024){
intBytes=Math.floor(grantdBytes/(1024*1024));
text+=intBytes+"GB";
}
if(grantdBytes>=1024){
intBytes=Math.floor(grantdBytes/(1024));
text+=intBytes+"GB";
}
text+=grantdBytes+"bytes";
document.getElementById("reuslt").innerHTML=text;
})
}
</script>
</head>
<body>
<form>
<input type="text" id="capacity" value="1024">
<input type="button" value="申请配额" onclick="getSize()">
</form>
<output id="reuslt"></output>
</body>
</html>
创建文件:
(1)磁盘配额申请成功后方能执行创建文件
(2)创建的文件存在于cookie中
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
function createFile(){
var size=document.getElementById("FileSize").value;
window.requestFileSystem=window.requestFileSystem || window.webkitRequestFileSystem;
window.webkitRequestFileSystem(PERSISTENT,size,function(fs){
var fileName=document.getElementById("FileName").value;
fs.root.getFile(fileName,{
create:true
},
function(fileEntry){
var text ="文件完整路径:"+fileEntry.fullPath+"<br/>";
text +="文件名"+fileEntry.name+"<br/>";
document.getElementById("result").innerHTML=text;
}
)
})
}
</script>
</head>
<body>
<form>
文件名:<input type="text" id="FileName" value="test.txt" /><br/>
文件大小:<input type="text" id="FileSize" value="1024" /><br/>
<input type="button" value="创建文件" onclick="createFile()" />
<output id="result"></output>
</form>
</body>
</html>
关键字词:html5,文件,api