将html表单数据写入txt文件而不使用Web服务器

use*_*844 7 html

我想知道是否有办法使用脚本提交/写入hxt表单数据到txt文件,但没有使用webserver,webhost,wamp,xamp等.

我一直在尝试使用php脚本,但他们只是在提交时打开php文档.

任何帮助表示赞赏:D

imu*_*ion 12

你可以使用JavaScript:

<script type ="text/javascript">
 function WriteToFile(passForm) {

    set fso = CreateObject("Scripting.FileSystemObject");  
    set s = fso.CreateTextFile("C:\test.txt", True);
    s.writeline(document.passForm.input1.value);
    s.writeline(document.passForm.input2.value);
    s.writeline(document.passForm.input3.value);
    s.Close();
 }
  </script>
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,则替代方法是ActiveX对象:

<script type = "text/javascript">
function WriteToFile(passForm)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\Test.txt", true);
s.WriteLine(document.passForm.input.value);
s.Close();
}
</script>
Run Code Online (Sandbox Code Playgroud)

不幸的是,据ActiveX我所知,该对象仅受支持ActiveX.

  • @TimMedora 很公平,我认为它确实有效。感谢您指出错误:) (2认同)

Dev*_*aig 11

像这样的东西?

<!DOCTYPE html>
<html>
<head>
<style>
form * {
  display: block;
  margin: 10px;
}
</style>
<script language="Javascript" >
function download(filename, text) {
  var pom = document.createElement('a');
  pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + 

encodeURIComponent(text));
  pom.setAttribute('download', filename);

  pom.style.display = 'none';
  document.body.appendChild(pom);

  pom.click();

  document.body.removeChild(pom);
}
</script>
</head>
<body>

<form onsubmit="download(this['name'].value, this['text'].value)">
  <input type="text" name="name" value="test.txt">
  <textarea rows=3 cols=50 name="text">Please type in this box. When you 

click the Download button, the contents of this box will be downloaded to 

your machine at the location you specify. Pretty nifty. </textarea>
  <input type="submit" value="Download">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)