【发布时间】:2016-11-17 08:55:27
【问题描述】:
我正在学习 javascript,因此我尝试使用 Javascript 将字符串附加到文本文件中。我尝试了以下但由于某种原因没有任何反应。我哪里做错了?我在 Mozilla firefox 浏览器上运行代码。
<%--
Document : demo
Created on : 17 Nov, 2016, 11:01:01 AM
Author : user
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<button onclick="WriteFile()">Click me</button>
<p id="demo"></p>
<script type="text/javascript">
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
function WriteFile()
{
var fh = fopen("C:\\javascriptlogs\\myfile.txt", 3); // Open the file for writing
if(fh!=-1) // If the file has been successfully opened
{
var str = "Some text goes here...";
fwrite(fh, str); // Write the string to a file
fclose(fh); // Close the file
}
}
</script>
</body>
</html>
【问题讨论】:
-
按 F12。看控制台。 Rethink your approach
-
我不知道javascript有
fopen函数? -
javascript 是客户端。
fwrite(),fclose()都是php .webdeveloper.com/forum/… -
据我所知,您将无法正常执行此操作(由于 Javascript 在任何计算机上写入文件的安全性问题,让它们成为您的服务器或客户端)。我见过 Javascript 直接写入服务器上文件的唯一方法是在节点服务器上运行应用程序。
标签: javascript