Created
October 10, 2021 07:18
-
-
Save devded/5ea2244aeb2c3dae531597f79e70fb04 to your computer and use it in GitHub Desktop.
React json file download and upload. Export a json object and download as a json file. Import a json file and process in client side.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
const FileProcess = () => { | |
const jsonFileDownload = () => { | |
const json_data = { | |
name: "Dedar", | |
age: "14", | |
address: "House #28", | |
}; | |
const fileName = "finename.json"; | |
const data = new Blob([JSON.stringify(json_data)], { type: "text/json" }); | |
const jsonURL = window.URL.createObjectURL(data); | |
const link = document.createElement("a"); | |
document.body.appendChild(link); | |
link.href = jsonURL; | |
link.setAttribute("download", fileName); | |
link.click(); | |
document.body.removeChild(link); | |
}; | |
const jsonFileUpload = (e) => { | |
const fileReader = new FileReader(); | |
fileReader.readAsText(e.target.files[0], "UTF-8"); | |
fileReader.onload = (e) => { | |
console.log("e.target.result", e.target.result); | |
const data = JSON.parse(e.target.result); | |
console.log("Json Data", data); | |
}; | |
}; | |
return ( | |
<div> | |
<> | |
<h2>Download JSON File</h2> | |
<button onClick={jsonFileDownload}>Download JSON File</button> | |
</> | |
<hr /> | |
<> | |
<h2>Upload JSON File</h2> | |
<input type="file" onChange={jsonFileUpload} /> | |
</> | |
</div> | |
); | |
}; | |
export default FileProcess; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment