I am trying to upload a file every which way but I cannot get it to work. The client side code looks like this:
var image = document.getElementById("upload").files[0];
I am grabbing the file in this way , I console log it and it works fine.
const request = axios.post(PROJECTS_URL, {avatar: image});
I send the post request and get a status 500. When I change the body of the post request to just text, and edit the server to take in text, it works fine.
I have also tried this:
var formData = new FormData();
formData.append("avatar", image, image.name)
const request = axios.post(PROJECTS_URL, formData);
for the server side code I am using multer and it looks like this:
app.post("/api", upload.single("avatar"), (req, res) => {
var entry = new Entry({
img: req.file.path
})
entry.save().then((data) => {
res.status(200).send(data)
}, (e) => {
res.send(e)
})
})
I set up the multer middleware just not including it. I am trying to upload an image to a mongodb database so I can later use the image url on the client side. I am really stuck here and would appreciate any suggestions.
In my console on chrom i see Content-Type: multipart/form-data; boundary=----WebKitFormBoundarye2QF7dzzJWEGUjJE; what is the boundary portion and is it preventing me from submiting the form?