Created
September 28, 2020 03:09
-
-
Save dev-sankhadip/bde920fd2ab8c6ccf6c0ba09e3ee197e to your computer and use it in GitHub Desktop.
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, { useState } from "react"; | |
import axios from "axios"; | |
function App() { | |
const [file, setFile] = useState(); | |
const [fileName, setFileName] = useState(""); | |
const saveFile = (e) => { | |
setFile(e.target.files[0]); | |
setFileName(e.target.files[0].name); | |
}; | |
const uploadFile = async (e) => { | |
const formData = new FormData(); | |
formData.append("file", file); | |
formData.append("fileName", fileName); | |
try { | |
const res = await axios.post( | |
"http://localhost:3000/upload", | |
formData | |
); | |
console.log(res); | |
} catch (ex) { | |
console.log(ex); | |
} | |
}; | |
return ( | |
<div className="App"> | |
<input type="file" onChange={saveFile} /> | |
<button onClick={uploadFile}>Upload</button> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment