Javascript中的文件管理

Javascript中的文件管理,javascript,file-management,Javascript,File Management,如何在Javascripton服务器中写入文件 我还需要读文件。 这是我的代码: function write() { var = true; if( var = true) { //write file } } function read() { //read file } 编写文件不是Javascript的一项功能。在最近的一些浏览器中,您已经可以阅读它们,但这不是一个好的选择。最好的方法是使用PHP读取它,并使用XMLHttpRequest获得响应 JavaScript PHP

如何在Javascripton服务器中写入文件

我还需要读文件。 这是我的代码:

function write()
{
var = true;
if( var = true)
{
//write file
    }
}
function read()
{
//read file
}

编写文件不是Javascript的一项功能。在最近的一些浏览器中,您已经可以阅读它们,但这不是一个好的选择。最好的方法是使用PHP读取它,并使用XMLHttpRequest获得响应

JavaScript

PHP


您可以通过执行可能带有文件名或id的AJAX请求来读取文件

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onload = function(){
    window.console.log(this.response);
}
xhr.open('GET','/readfile.php?id=1234');
xhr.send();
您可以通过从可能为text类型的输入获取数据来写入文件。假设输入id为文本

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onload = function(){
    window.console.log(this.response);
}
xhr.open('POST','/write.php');
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("id=someid&text=" + document.getElementById("text").value);
在php端,只需获取post数据并将其写入文件

$id = $_REQUEST["id"];
$text = $_REQUEST["text"]
$file = fopen($id . ".txt","w"); // you can change w to a incase you want to append to existing content in the file
fwrite($file,$text);
fclose($file);

如果你想让Javascript读写,据我所知,只有一个HTML5文件API,但我想那只是用来读文件的。

如果我正确理解了你的问题,你希望用服务器读写文件,你的服务器端语言是Javascript。 如果您使用的是节点,则此链接:
提供有关执行相同操作的相关信息

以及如何用php、asp、jsp编写文件。等等@Wolf:你不能在客户端写文件,超出浏览器自身的下载能力。这是一个安全问题。你喜欢点击一个链接到一个运行一些js的页面,并将任何东西写入你的系统吗?这段时间,故事的结尾,希望它能一直保持下去;我想在服务器上写文件,文件的内容是什么?用户在textarea上写入的内容?@MarkLinus no,ifblablabla=true{//write blabla to file}在这种情况下,您可以使用浏览器中运行的javascript编写服务器端文件内容,除非您具有接收HTTP请求的显式服务器端脚本,并在处理该请求后将其写入文件。之前发布的解决方案只是假设服务器端语言是PHP。在将content server写入文件系统之前,请务必记住相关的安全隐患,并对服务器上的content server执行适当的清理。
var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onload = function(){
    window.console.log(this.response);
}
xhr.open('POST','/write.php');
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("id=someid&text=" + document.getElementById("text").value);
$id = $_REQUEST["id"];
$text = $_REQUEST["text"]
$file = fopen($id . ".txt","w"); // you can change w to a incase you want to append to existing content in the file
fwrite($file,$text);
fclose($file);