Javascript 文件变量声明/创建错误:此对象不支持此操作

Javascript File variable declaration/creation Error: This Object does not support this action

本文关键字:对象 不支持 操作 错误 创建 文件 变量 声明 Javascript      更新时间:2023-09-26

今天我创建了一个HTML文档来编写登录和注册网站。我的想法如下:创建帐户时,您的数据应保存在文本文件中(不安全)。登录时,代码应将输入与文件中的数据进行比较。但是,当我想为我的文件初始化变量时,Internet Explorer 显示以下错误:SCRIPT445:此对象不支持此操作。我这里有标记的行:

//Here is the layout creation...
<script>
  function login() 
  {
    var username = document.getElementById("txtusername").value;
    var kennwort = document.getElementById("txtpassword").value;
  }
  function register()
  {
    var rusername = document.getElementById("txtrusername").value;
    var remail = document.getElementById("txtremail").value;
    var rkennwort = document.getElementById("txtrpassword").value;
    var rconfirmpassword = document.getElementById("txtrconfirmpassword").value;
    if(rpassword!=rconfirmpassword)
    {
      alert(unescape("The passwords do not match!", "Error"));
      return;
    }
    //This line creates the error
    var file = new File("C:/Users/Konsicoder/Desktop/Data/data.txt");
    //This line creates the error
    alert("Success!", "Success");
  }
</script>
您可能需要

使用浏览器文件选择器才能首先获取本地文件的句柄,而不是提供本地文件系统路径并实例化 File 对象。

[http://www.html5rocks.com/en/tutorials/file/dndfiles/][1][使用 File API 读取 JavaScript 中的文件][1]

这是一篇关于 SO 的文章,详细介绍了如何实例化 File 对象:如何在 JavaScript 中实例化 File 对象?