📌  相关文章
📜  如何在没有 nodejs 的情况下使用 javascript 写入文件 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:42.895000             🧑  作者: Mango

代码示例1
function convertToJSON() {
  var firstname = document.getElementById('firstname').value;
  var lastname = document.getElementById('lastname').value;
  var email = document.getElementById('email').value;

  var jsonObject = {
    "FirstName": firstname,
    "LastName": lastname,
    "email": email
  }

  document.getElementById('output').value = JSON.stringify(jsonObject)
}

function saveToFile() {
  convertToJSON();
  var jsonObjectAsString = document.getElementById('output').value;

  var blob = new Blob([jsonObjectAsString], {
    //type: 'application/json'
    type: 'octet/stream'
  });
  console.log(blob);

  var anchor = document.createElement('a')
  anchor.download = "user.json";
  anchor.href = window.URL.createObjectURL(blob);
  anchor.innerHTML = "download"
  anchor.click();

  console.log(anchor);

  document.getElementById('output').append(anchor)


}