保存文件和本地下载

时间:2018-10-14 21:59:43

标签: javascript arrays downloading

大家好。我想创建一个文件,并且能够在文件被保存/更新后下载,而无需像php或sql这样的后端,而只需要本地存储数据。这是我的代码,可以下载初始文件,但似乎无法获取更新和保存的文件。希望可以有人帮帮我。我应该放弃这个,而只是去PHP或SQL吗?我非常感谢您的帮助。谢谢

function savePerson() {
  var person = {
    dateOfConsult: document.getElementById('date').innerHTML,
    name: document.getElementById('txtname').innerHTML
  };
}
savePerson()

function downloadCSV(csv, filename) {
  var csvfile;
  var downloadLink;

  csvfile = new Blob([csv], {
    type: "text/csv"
  });
  downloadLink = document.createElement("a");
  downloadLink.download = filename;
  downloadLink.href = window.URL.createObjectURL(csvfile);
  downloadlink.style.display = "none";
  document.body.appendChild(downloadLink);
  downloadLink.click()
}

function exportTable to csv(filename) {
    var csv = []
    var rows = document.querySelectorAll("table tr");
    for (var i = 0; i < rows.length; i++) {
      var row = [],
        cols = rows[i].querySelectorAll("td, tr");
      for (var j = 0; j < cols.length; j++)
        row.push(cols[j].innerText);
      csv.push(row.join(","));
    }

    downloadCSV(csv.join("\n"), filename);
<h3>Profile</h3>
<form action="#" name="Profile" onsubmit="savePerson()">
  <table align="center">
    <tr>
      <td>Date of</td>
      <td><input type="text" id="date" size="30"></td>
    </tr>
    <tr>
      <td>Name</td>
      <td><input type="textname" name="name" id="textname" size="30"></td>
    </tr>

0 个答案:

没有答案