使用 CGI 和 Javascript 保存文件

发布于 2024-12-10 09:41:04 字数 220 浏览 0 评论 0原文

我有一些 JSON 脚本,我计划在网站上解析它们,然后允许客户端通过我的界面编辑它们(稍后再次加载和解析以进行显示)。问题是,Javascript 无法写入文件系统。我已经有了一个使用 Javascript(和 jQuery)读取 JSON 文件的系统。现在,我听说以后可以使用 CGI 来保存数据。有人可以给我一些参考资料和深入的解释吗?我读过一些关于 CGI 的一般知识,但没有具体的内容。

感谢您的帮助!

I have some JSON scripts that I plan on parsing on the site and then allowing the clients to edit them through my interface (to later be loaded and parsed once again for display). Problem is, Javascript doesn't have access to writing to the file system. I've already got a system for reading the JSON files using Javascript (and jQuery). Now, I've heard I can use CGI to save the data later. Can somebody give me some references and in depth explanations? I've read a bit about CGI in general but nothing specific.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2024-12-17 09:41:04

CGI 是服务器与脚本交互的一种方式。基本上,您只需设置服务器来执行一个文件,它将通过设置多个环境变量并将 POST 数据馈送到其标准输入来执行该文件。该脚本应输出页面的标题,然后是内容。

CGI 脚本可以用多种不同的语言编写。 Perl 以 CGI 脚本而闻名; 此处有相关文档。 Python 有一个cgi 模块来处理CGI。 Ruby 有 一个 CGI也是如此。

下面是一个用 Python 编写的快速 CGI 脚本,用于写入文件。您可能想要修改它或将其用作参考,而不是按原样使用它:

#!/usr/bin/env python
import os
import os.path
import sys
import json
import cgi
# You'll probably want to remove or alter
# the following line for production.
import cgitb; cgitb.enable()

def bad_request():
    print "Status: 400 Bad Request"
    print "Content-Type: application/json"
    print ""
    json.dump({'success': False}, sys.stdout)
    sys.exit(0)

assert 'REQUEST_METHOD' in os.environ
if os.environ['REQUEST_METHOD'] != 'POST':
    bad_request()

form = cgi.FieldStorage()
if 'data' not in form:
    bad_request()

filename = os.path.join(os.path.dirname(__file__), "some_file.json")
with open(filename, "wb") as f:
    f.write(form['data'].value)

print "Content-Type: application/json"
print ""
json.dump({'success': True}, sys.stdout)

如果您使用 data 参数POST 到它,它将保存该数据数据到与其自身相同的目录中的 some_file.json 中。

CGI is a way for servers to interface with scripts. Pretty much, you just set up the server to execute a file, and it will execute it with several environment variables set and POST data fed to its standard input. The script should output the headers for the page, followed by the content.

CGI scripts can be written in many different languages. Perl is well-known for CGI scripts; it has documentation on it here. Python has a cgi module to deal with CGI. Ruby has a CGI package as well.

Here's a quick CGI script written in Python that writes to a file. You'll probably want to modify it or use it as a reference rather than using it as-is:

#!/usr/bin/env python
import os
import os.path
import sys
import json
import cgi
# You'll probably want to remove or alter
# the following line for production.
import cgitb; cgitb.enable()

def bad_request():
    print "Status: 400 Bad Request"
    print "Content-Type: application/json"
    print ""
    json.dump({'success': False}, sys.stdout)
    sys.exit(0)

assert 'REQUEST_METHOD' in os.environ
if os.environ['REQUEST_METHOD'] != 'POST':
    bad_request()

form = cgi.FieldStorage()
if 'data' not in form:
    bad_request()

filename = os.path.join(os.path.dirname(__file__), "some_file.json")
with open(filename, "wb") as f:
    f.write(form['data'].value)

print "Content-Type: application/json"
print ""
json.dump({'success': True}, sys.stdout)

If you POST to it with a data parameter, it will save that data into some_file.json in the same directory as itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文