cockpit可以让用户对完整的读写和查看常规文件,但是对大型文件的读写支持的不是特别好。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| file = cockpit.file(path, { syntax: syntax_object, binary: boolean, max_read_size: int, superuser: string, host: string })
promise = file.read() promise .then((content, tag) => { ... }) .catch(error => { ... })
promise = file.replace(content, [ expected_tag ]) promise .then(new_tag => { ... }) .catch(error => { ... })
promise = file.modify(callback, [ initial_content, initial_tag ] promise .then((new_content, new_tag) => { ... }) .catch(error => { ... })
file.watch((content, tag, [error]) => { })
file.close()
|
简单的读取功能,text1为textarea控件id
1 2 3 4 5 6 7
| cockpit.file("/root/test").read() .then((content, tag) => { text1.value = content; }) .catch(error => { result.innerHTML ="0"; });
|
简单的写入
1 2 3 4 5 6
| cockpit.file("/root/test").replace(content) .then(tag => { }) .catch(error => { result.innerHTML ="0"; });
|
然后调用close()将文件关闭。