It's unclear what you're trying to do with your setFormValues()
function as you don't show the whole context here. But, what you should do is at the point in time when you want to do the upload, you just go get the file data directly from the form.
Here are three ways to do it, all of which I've tested and work:
Let Browser Upload the Form
<form id="myForm" action="/upload" enctype="multipart/form-data" method="post">
<label class="custom-uploader" for="file">Upload Your File</label>
<input id="file" accept="image/jpeg,image/gif,image/png,application/pdf,image/x-eps" name="fileToUpload" type="file" />
<br><button class="btn btn-success" name="submit" type="submit">
Upload File
</button>
</form>
Create FormData object from the Entire Form, Upload with Axios
<form id="myForm" action="/upload" enctype="multipart/form-data" method="post">
<label class="custom-uploader" for="file">Upload Your File</label>
<input id="file" accept="image/jpeg,image/gif,image/png,application/pdf,image/x-eps" name="fileToUpload" type="file" />
<br><button id="axiosButton1" class="btn btn-success" name="submitAxios">
Upload File via Axios whole FormData
</button>
</form>
<script>
// button1, create FormData from the whole form
document.getElementById("axiosButton1").addEventListener("click", e => {
e.preventDefault();
const formData = new FormData(document.getElementById("myForm"))
axios.post("/upload", formData).then(result => {
console.log(result);
alert("upload complete");
}).catch(err => {
console.log(err);
alert("upload error");
})
});
</script>
Create FormData object manually and just append the file object to it, Upload with Axios
<form id="myForm" action="/upload" enctype="multipart/form-data" method="post">
<label class="custom-uploader" for="file">Upload Your File</label>
<input id="file" accept="image/jpeg,image/gif,image/png,application/pdf,image/x-eps" name="fileToUpload" type="file" />
<br><button id="axiosButton2" class="btn btn-success" name="submitAxios">
Upload File via Axios created FormData
</button>
</form>
<script>
// button2, manually created FormData with just files added to it
document.getElementById("axiosButton2").addEventListener("click", e => {
console.log("axiosButton2");
e.preventDefault();
const fileItem = document.getElementById("file");
const formData = new FormData();
formData.append("fileToUpload", fileItem.files[0]);
axios.post("/upload", formData).then(result => {
console.log(result);
alert("upload complete xxx");
}).catch(err => {
console.log(err);
alert("upload error");
})
});
</script>
Back End
And, all three of these options work with this backend code where the uploaded file is deposited into the uploads/
directory:
import express from 'express';
import multer from 'multer';
const upload = multer({ dest: 'uploads/' });
import path from 'path';
const app = express();
app.get("/", (req, res) => {
res.sendFile(path.resolve("index.html"));
});
app.post("/upload", upload.single('fileToUpload'), (req, res) => {
console.log(req.file);
res.send("upload complete");
});
app.listen(80);