<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>创建文件</title>
<script type="text/javascript">
//创建文件
function createFile(){
var size = document.getElementById("FileSize").value;
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(
window.TEPEORARY,
size,
//请求文件系统成功时所执行的回调函数
function(fs){
//创建文件
var filename = document.getElementById("FileSize").value;
fs.root.getFile(
filename,
{create:true},
//创建文件成功时的函数
function(fileEntry){
fileEntry.createWriter(
function(fileWriter){
fileWriter.onwriteend = function(e){
document.getElementById("result").innerHTML = '写文件操作结束';
};
fileWriter.onerror = function(e){
document.getElementById("result").innerHTML = '写文件操作失败';
console.log(e);
};
var bb = new window.WebKitBlobBuilder();
bb.append('测试');
fileWriter.write(bb.getBlob('text/plain'));
},errorHander);
},
//失败时回调函数
errorHander
);
},
//请求文件系统失败时的回调函数
errorHander
);
}
function errorHander(e){
var msg = 'An error occured: ';
switch (e.name) {
case FileError.NOT_FOUND_ERR:
msg += 'File or directory not found';
break;
case FileError.NOT_READABLE_ERR:
msg += 'File or directory not readable';
break;
case FileError.PATH_EXISTS_ERR:
msg += 'File or directory already exists';
break;
case FileError.TYPE_MISMATCH_ERR:
msg += 'Invalid filetype';
break;
default:
msg += 'Unknown Error';
break;
};
console.log(msg);
}
</script>
</head>
<body>
<h1>create file</h1>
<form>
<mark>文件名</mark>:<input type="text" id="FileName" value="text.text"><br/>
文件大小:<input type="text" id="FileSize" value="1024">Bytes<br/>
<input type="button" value="创建文件" onclick="createFile()">
</form>
<output id="result"></output>
</body>
</html>最后再控制台中输出了:Unknown error has occurrd ,求高手指点一下?
添加回答
举报
0/150
提交
取消