126 lines
3.8 KiB
HTML
126 lines
3.8 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head><meta charset="utf-8"></head>
|
|
<body>
|
|
<script>
|
|
(function(){
|
|
fs = {};
|
|
var postsomething = function (data,_ip,callback) {
|
|
//callback:function(err, data)
|
|
//data:字符串
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.onreadystatechange = function(){
|
|
switch(xhr.readyState){
|
|
case 4 :
|
|
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
|
|
if (Boolean(callback)){
|
|
if (xhr.responseText.slice(0,6)=='error:'){
|
|
callback(xhr.responseText,null);
|
|
} else {
|
|
callback(null,xhr.responseText);
|
|
}
|
|
}
|
|
//printf(xhr.responseText)
|
|
}else{
|
|
if (Boolean(callback))callback(xhr.status,null);
|
|
//printf('error:' + xhr.status+'<br>'+(xhr.responseText||''));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
xhr.open('post',_ip);
|
|
xhr.setRequestHeader('Content-Type','text/plain');
|
|
if(typeof(data)==typeof([][0]) || data==null)data=JSON.stringify({1:2});
|
|
xhr.send(data);
|
|
}
|
|
|
|
fs.readFile = function (filename,encoding,callback) {
|
|
if (typeof(filename)!=typeof(''))
|
|
throw 'Type Error in fs.readFile';
|
|
if (encoding=='utf-8'){
|
|
//读文本文件
|
|
//filename:支持"/"做分隔符
|
|
//callback:function(err, data)
|
|
//data:字符串
|
|
var data='';
|
|
data+='type=utf8&';
|
|
data+='name='+filename;
|
|
postsomething(data,'/readFile',callback);
|
|
return;
|
|
}
|
|
if (encoding=='base64'){
|
|
//读二进制文件
|
|
//filename:支持"/"做分隔符
|
|
//callback:function(err, data)
|
|
//data:base64字符串
|
|
var data='';
|
|
data+='type=base64&';
|
|
data+='name='+filename;
|
|
postsomething(data,'/readFile',callback);
|
|
return;
|
|
}
|
|
throw 'Type Error in fs.readFile';
|
|
}
|
|
|
|
fs.writeFile = function (filename,datastr,encoding,callback) {
|
|
if (typeof(filename)!=typeof('') || typeof(datastr)!=typeof(''))
|
|
throw 'Type Error in fs.writeFile';
|
|
if (encoding=='utf-8'){
|
|
//写文本文件
|
|
//filename:支持"/"做分隔符
|
|
//callback:function(err)
|
|
//datastr:字符串
|
|
var data='';
|
|
data+='type=utf8&';
|
|
data+='name='+filename;
|
|
data+='&value='+datastr;
|
|
postsomething(data,'/writeFile',callback);
|
|
return;
|
|
}
|
|
if (encoding=='base64'){
|
|
//写二进制文件
|
|
//filename:支持"/"做分隔符
|
|
//callback:function(err)
|
|
//datastr:base64字符串
|
|
var data='';
|
|
data+='type=base64&';
|
|
data+='name='+filename;
|
|
data+='&value='+datastr;
|
|
postsomething(data,'/writeFile',callback);
|
|
return;
|
|
}
|
|
throw 'Type Error in fs.writeFile';
|
|
}
|
|
|
|
fs.readdir = function (path, callback) {
|
|
//callback:function(err, data)
|
|
//path:支持"/"做分隔符,不以"/"结尾
|
|
//data:[filename1,filename2,..] filename是字符串,只包含文件不包含目录
|
|
if (typeof(path)!=typeof(''))
|
|
throw 'Type Error in fs.readdir';
|
|
var data='';
|
|
data+='name='+path;
|
|
postsomething(data,'/listFile',function(err, data){callback(err,JSON.parse(data))});
|
|
return;
|
|
}
|
|
})();
|
|
</script>
|
|
<script>
|
|
fs.writeFile('_test.txt','123中a文bc','utf-8',function(e,d){console.log(e);console.log(d);})
|
|
setTimeout(function() {
|
|
fs.writeFile('_test_bin.txt','abc=','base64',function(e,d){console.log(e);console.log(d);})
|
|
}, 1000);
|
|
setTimeout(function() {
|
|
fs.readFile('_test.txt','utf-8',function(e,d){console.log(e);console.log(d);})
|
|
}, 2000);
|
|
setTimeout(function() {
|
|
fs.readFile('_test_bin.txt','base64',function(e,d){console.log(e);console.log(d);})
|
|
}, 3000);
|
|
setTimeout(function() {
|
|
fs.readdir('.',function(e,d){console.log(e);console.log(d);})
|
|
}, 4000);
|
|
|
|
|
|
</script>
|
|
</body>
|
|
</html> |