Search code examples
javascriptreactjsfile-uploadfilepathjson-server

How can I upload a file in a reactjs and save the path to the file in the JSON server?


What should i do here to get the path const [file, setFile] = useState("");

function handleUpload(e) {
     setFile(e.target.files[0]);
//what should i do here to get the path
}
 <Button variant="contained" component="label" >Upload File <input type="file" hidden  webkitdirectory onChange={(e)=>handleUpload(e)}/></Button>

Solution

  • For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:

    possible of duplicate