简体   繁体   English

使用JavaScript,可以在服务器上的文件中读取/写入文件

[英]With JavaScript is it possible to Read/Write from/to a file on the server

I have a series of JSON Objects I want to save locally on my server. 我有一系列的JSON对象,我想在服务器上本地保存。 I am attempting to avoid any server-side script like PHP as required per demand of whats being built. 我正在尝试根据所构建内容的需求来避免任何服务器端脚本(如PHP)。 I know its a security risk, but that in this case is not a particular worry. 我知道它有安全隐患,但是在这种情况下,您不必特别担心。 So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery? 这样说是否有可能实际上在不借助诸如jquery之类的lib的帮助下,通过javascript实际写入文件或获取文件内容?

I should mention I am attempting to avoid ActiveX as I know this is an IE only feature and the software we are developing is planned to be Cross Browser supported 我应该提到我正在尝试避免ActiveX,因为我知道这是仅IE的功能,并且我们正在开发的软件计划受跨浏览器支持

So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery? 这样说是否有可能实际上在不借助诸如jquery之类的lib的帮助下,通过javascript实际写入文件或获取文件内容?

Nope. 不。 You will need something running on server side that can receive your JavaScript input and write it to the server. 您将需要在服务器端运行一些可以接收JavaScript输入并将其写入服务器的内容。

Internet Explorer's proprietary file writing functionality is for writing local (client-side) files only. Internet Explorer的专有文件写入功能仅用于写入本地(客户端)文件。

You can read a file using ajax, but without a server side language you cannot write a file to the server. 您可以使用ajax读取文件,但是如果没有服务器端语言,则无法将文件写入服务器。

https://developer.mozilla.org/en/ajax https://developer.mozilla.org/en/ajax

No. Javascript runs on the client. 否。JavaScript在客户端上运行。 You need server-side code to access the server's file system. 您需要服务器端代码才能访问服务器的文件系统。

Client-side JavaScript can only send data to a server, there's no way for it to tell the server what to do with the data. 客户端JavaScript只能将数据发送到服务器,它无法告诉服务器如何处理数据。

To save data to a file or db on a server, you'll require a server-side script of some sort ( could be server-side JS with Node.js). 要将数据保存到服务器上的文件或数据库中,您将需要某种服务器端脚本( 可以是带有Node.js的服务器端JS)。 If all you need is persistent data, you could store some JSON strings in localStorage or in cookies as needed. 如果仅需要持久性数据,则可以根据需要将一些JSON字符串存储在localStorage或cookie中。 They wouldn't be shareable that way though. 他们不会那样共享。

Yes, you can use AJAX requests in JavaScript without using jQuery. 是的,您可以在JavaScript中使用AJAX请求,而无需使用jQuery。 However, jQuery will save you an ungodly amount of time and cross-browser testing. 但是,jQuery将为您节省大量时间和跨浏览器测试。

But, as others have already said, you can't write server files without server code. 但是,正如其他人已经说过的那样,没有服务器代码就无法编写服务器文件。

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

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