有什么方法可以使用浏览器 javascript 将 json 保存到一个文件和 read/update 同一个文件?
Is there any way to save json to a file and read/update the same file using browser javascript?
// LocalStorage is a lowdb adapter for saving to localStorage
const adapter = new LocalStorage('db')
// Create database instance
const db = low(adapter)
// Set default state
db.defaults({ items: [] })
.write()
现在如何像 './maindb.json'
那样将其保存在托管位置?
有没有类似 FileSync 的东西可用?
Update
意识到客户端无法访问服务器文件系统后。我可能问错了问题。但是有一个使用 fetch 的解决方案。所以如果有人有答案,请把它记下来,我正在尽力。
index.html -> fetch(jsonObj) -> post -> save.php-> creates maindb.json/updates.
index.html -> fetch() -> get -> save.php -> returns from maindb.json.
Controller.php
<?php
class Controller {
private $file = 'data.json',
$jsondata = null,
$tempArray = array(),
$data= array();
public function __construct() {
$this->init();
}
private function init(){
if (!file_exists($this->file)) {
touch($this->file);
$this->read();
}else{
$this->read();
}
}
private function read(){
$this->jsondata = file_get_contents($this->file);
$this->tempArray = $this->jsondata != null ? json_decode($this->jsondata): array();
}
private function write(){
array_push($this->tempArray, $this->data);
$this->jsondata = json_encode($this->tempArray);
file_put_contents($this->file, $this->jsondata);
}
public function push($data){
$this->data = $data;
if($this->data){
$this->write();
return true;
}
}
public function get(){
$this->read();
return json_encode($this->tempArray);
}
}
?>
json.php
<?php
require 'Controller.php';
$db = new Controller;
if($db->push($_POST['data'])){
echo $db->get();
}
?>
javascript
function post(input){
var data = {'data': input}
$.ajax({
type: 'POST',
url: 'json.php',
dataType: 'json',
data: data,
success: function(data) {
console.log(data)
}
});
}
// LocalStorage is a lowdb adapter for saving to localStorage
const adapter = new LocalStorage('db')
// Create database instance
const db = low(adapter)
// Set default state
db.defaults({ items: [] })
.write()
现在如何像 './maindb.json'
那样将其保存在托管位置?
有没有类似 FileSync 的东西可用?
Update
意识到客户端无法访问服务器文件系统后。我可能问错了问题。但是有一个使用 fetch 的解决方案。所以如果有人有答案,请把它记下来,我正在尽力。
index.html -> fetch(jsonObj) -> post -> save.php-> creates maindb.json/updates.
index.html -> fetch() -> get -> save.php -> returns from maindb.json.
Controller.php
<?php
class Controller {
private $file = 'data.json',
$jsondata = null,
$tempArray = array(),
$data= array();
public function __construct() {
$this->init();
}
private function init(){
if (!file_exists($this->file)) {
touch($this->file);
$this->read();
}else{
$this->read();
}
}
private function read(){
$this->jsondata = file_get_contents($this->file);
$this->tempArray = $this->jsondata != null ? json_decode($this->jsondata): array();
}
private function write(){
array_push($this->tempArray, $this->data);
$this->jsondata = json_encode($this->tempArray);
file_put_contents($this->file, $this->jsondata);
}
public function push($data){
$this->data = $data;
if($this->data){
$this->write();
return true;
}
}
public function get(){
$this->read();
return json_encode($this->tempArray);
}
}
?>
json.php
<?php
require 'Controller.php';
$db = new Controller;
if($db->push($_POST['data'])){
echo $db->get();
}
?>
javascript
function post(input){
var data = {'data': input}
$.ajax({
type: 'POST',
url: 'json.php',
dataType: 'json',
data: data,
success: function(data) {
console.log(data)
}
});
}