“Pobierz plik JSON React” Kod odpowiedzi

Pobierz plik JSON React


  const exportData = () => {
    const jsonString = `data:text/json;chatset=utf-8,${encodeURIComponent(
      JSON.stringify(data)
    )}`;
    const link = document.createElement("a");
    link.href = jsonString;
    link.download = "data.json";

    link.click();
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button type="button" onClick={exportData}>
        Export Data
      </button>
    </div>
  );
}
Daniel Kristoffersen

Donwload Dane z React JS w pliku JSON

const downloadFile = async () => {
  const {myData} = this.state; // I am assuming that "this.state.myData"
                               // is an object and I wrote it to file as
                               // json
  const fileName = "file";
  const json = JSON.stringify(myData);
  const blob = new Blob([json],{type:'application/json'});
  const href = await URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = href;
  link.download = fileName + ".json";
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
Perfect Panther

Odpowiedzi podobne do “Pobierz plik JSON React”

Pytania podobne do “Pobierz plik JSON React”

Więcej pokrewnych odpowiedzi na “Pobierz plik JSON React” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu