📌  相关文章
📜  如何在 react 中下载来自后端的 json 对象 - Javascript 代码示例

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

代码示例1
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);
}