Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
How to push data to local JSON file on button click using JavaScript?

index.html

<button click="submit_data" id="submit">Submit</button>

script.js

document.getElementById('submit').addEventListener('click', function () {
    task = document.getElementById('new_task').value
    task_deadline = document.getElementById('task_deadline').value
    let push_data = {
        "item": task,
        "deadline": task_deadline
    }
    // FileSystem.writeFile("data.json", JSON.stringify(push_data));
})

I have a json file in the same directory and I am trying to push push_data to that file, but I cannot seem to find a solution. I am not using any framework such as Angular or React. Is it possible using plain javascript? If not, what are the possible solutions?

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You just need to use the API designed for this purposes

https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API#writing_to_files

async function saveFile(blob) {
  // create a new handle
  const newHandle = await window.showSaveFilePicker();
  // create a FileSystemWritableFileStream to write to
  const writableStream = await newHandle.createWritable();
  // write our file
  await writableStream.write(blob);
  // close the file and write the contents to disk.
  await writableStream.close();
}

function save () {
    let push_data = {
        "item": "test",
        "deadline": "test2"
    }
    saveFile(JSON.stringify(push_data)).then(console.log).catch(console.error); 
}

save();

Note:

This API opens up potential functionality the web has been lacking. Still, security has been of utmost concern when designing the API, and access to file/directory data is disallowed unless the user specifically permits it.

about 4 years ago · Santiago Gelvez Relatório

0

You can create a URL to download. A dataURL. See also working fiddle

var storageObj = {
  "hello": "world"
}

function download_json() {
  var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(storageObj));
  var dlAnchorElem = document.querySelector('.dummy');
  dlAnchorElem.setAttribute("href", dataStr);
  dlAnchorElem.setAttribute("download", "scene.json");
  dlAnchorElem.click();
}
Click to download JSON on a non-sandbox environment 
<form onsubmit="download_json(); return false">
  <button click="submit_data" id="submit">Submit</button>
</form>

<a class="dummy"></a>

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda