简体   繁体   English

写入本地JSON文件javascript

[英]Write to a local JSON file javascript

I'm trying to write to my local JSON file with javascript, I'm getting it from my HTML, changing it and want to write it back. 我正在尝试使用javascript写入本地JSON文件,我从HTML中获取了该文件,对其进行了更改并希望将其写回。 I found how to read it from here but didn't find how to write to it back to the json file. 我找到了如何从这里读取它,但是没有找到如何将其写回到json文件中。

Note: I want to this without Node.js , 'fs' won't help.. 注意:我想要没有Node.js的情况,“ fs”将无济于事。

My code to get the JSON file: 我的获取JSON文件的代码:

<script type="text/javascript" src="data.json></script>
<script type="text/javascript" src="javascrip.js"></script>

var mydata = JSON.parse(data);

Then I changed the value of the 'name', for example. 然后,例如,我更改了“名称”的值。

mydata["name"] = "NewName";

And then I want to send it back to the json file, update it. 然后我想将其发送回json文件,对其进行更新。

I didn't really find any code to do so. 我真的没有找到任何代码可以这样做。

You can't write to file system due to security constraints of the browser. 由于浏览器的安全性限制,您无法写入文件系统。

You can do that only this way - https://stackoverflow.com/a/30800715/6149665 您只能通过这种方式执行此操作-https: //stackoverflow.com/a/30800715/6149665

As ArtemSky said, You can't write to the local file system. 正如ArtemSky所说,您无法写入本地文件系统。 The way to accomplish what you want to do would be to use a server that can write to it's local file system or a database or whatever. 完成您要执行的操作的方法是使用可以写入其本地文件系统或数据库等的服务器。

So you would want to have the data stored somewhere, either a local file on the server, in the cloud, etc. or a database of some sort. 因此,您希望将数据存储在某个位置,例如服务器上的本地文件,云中的等等,或某种数据库。 Then you would set up an API on the server that you could call remotely to get the data via an XMLHttpRequest(XHR) 然后,您将在服务器上设置一个可以远程调用的API,以通过XMLHttpRequest(XHR)获取数据。

Then you would create another API method you can use to send the data back and then call that with the updated/new data. 然后,您将创建另一个API方法,可用于将数据发送回去,然后使用更新的/新的数据进行调用。

Writing to the local file system is a security concern because if anyone who can write code could overwrite your system files otherwise. 写入本地文件系统是一个安全问题,因为如果有人可以编写代码,则可能会覆盖您的系统文件。 Preventing the ability to write to the local file system is the only way to make the web safe. 阻止写入本地文件系统的能力是使Web安全的唯一方法。

While you can't directly write to the file system due because of security constraints, you can trigger a save dialog to request the user to save the file. 尽管由于安全限制而无法直接写入文件系统,但是您可以触发一个保存对话框来请求用户保存文件。 I use this function which works on all recent releases of the major browsers. 我使用了该功能,该功能可在所有主要浏览器的所有最新版本中使用。

function download(data, filename) {
    // data is the string type, that contains the contents of the file.
    // filename is the default file name, some browsers allow the user to change this during the save dialog.

    // Note that we use octet/stream as the mimetype
    // this is to prevent some browsers from displaying the 
    // contents in another browser tab instead of downloading the file
    var blob = new Blob([data], {type:'octet/stream'});

    //IE 10+
    if (window.navigator.msSaveBlob) {
        window.navigator.msSaveBlob(blob, filename);
    }
    else {
        //Everything else
        var url = window.URL.createObjectURL(blob);
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.href = url;
        a.download = filename;

        setTimeout(() => {
            //setTimeout hack is required for older versions of Safari

            a.click();

            //Cleanup
            window.URL.revokeObjectURL(url);
            document.body.removeChild(a);
        }, 1);
    }
}

It is also worth mentioning that HTML5 spec has a download attribute, but it isn't currently supported on IE. 还值得一提的是HTML5规范具有download属性,但IE当前不支持该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM