Skip to content

Instantly share code, notes, and snippets.

@dev-sankhadip
Created September 28, 2020 03:09
Show Gist options
  • Save dev-sankhadip/bde920fd2ab8c6ccf6c0ba09e3ee197e to your computer and use it in GitHub Desktop.
Save dev-sankhadip/bde920fd2ab8c6ccf6c0ba09e3ee197e to your computer and use it in GitHub Desktop.
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