node.jsexpresspostfile-uploadaxios

Can't upload file using express-fileupload


I'm using express-fileupload module to parse uploaded file. Upload is done with axios.

const formData = new FormData();
formData.append("file", data.gameCover);
formData.append("gameTitle", data.gameTitle);
formData.append("gamePrice", data.gamePrice);
formData.append("description", data.description);

return axios.post(apiUrl + "/games/add", formData).then(res => {
  dispatch({ type: ADD_GAME, payload: res.data.game });
});

This is the POST request

Serverside code looks like this:

router.use(fileUpload());

router.post("/add", (req, res) => {
  if (!req.files) return res.status(400).send("No files were uploaded.");

Of course I'm getting "No files were uploaded" when trying to upload.


Solution

  • Finally after debugging step after step found that data.gameCover is an array so that's the solution

    formData.append("file", data.gameCover[0]);