IE瀏覽器利用ActiveXObject物件對檔案系統進行操作
如果要用網頁做一個客戶端的程式(無後臺),那麼就要能對檔案系統進行操作。想實現的功能如下:引數配置物件轉換成json字串儲存到檔案系統, 從檔案系統讀取字串轉換成json物件。
CreateTextFile(FileName, Overwrite, Unicode)
OpenTextFile(FileName, IOMode, Create, Format)
var fso = new ActiveXObject("Scripting.FileSystemObject"); //載入控制元件
var path = "C:\\Users\\gaohu\\Desktop\\新建資料夾\\a.txt" ;
function readFile(fso,path){
var f1 = fso.GetFile(path);
var fh = fso.OpenTextFile(f1, 1/*reading*/);
var content = '';
while ( !fh.AtEndOfStream ) {
content += fh.ReadLine();
}
fh.close();
return JSON.parse(content);
}
function writeFile(fso,path){
var a = [{"person1" :{
"name":"abc",
"age":10
}},{"person2":{
"name":"cde",
"age":20
}}];
var str = JSON.stringify(a);
newFile = fso.CreateTextFile(path, true);
newFile.WriteLine(str);
newFile.close();
}