0

Hi I am trying to upload a file from fronend to backend using multer

Front End

var formData = new FormData();
 formData.append("files", image[0]);
 formData.append("data", JSON.stringify({status: 'ACK', message: "You are fu*ked"});
return http.axios
  .post(apiUrl + apiDomain + "/createAnnouncement", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  })
  .then((res) => res);

Backend

const bodyParser = require("body-parser");
const Express = require("express");
var storage = multer.diskStorage({
 destination: function (req, file, cb) {
cb(null, config.get("announcemnt_upload_file_storage"));
},
filename: function (req, file, cb) {
cb(null, file.filename + "_" + Date.now());
   },
});
var upload = multer({ storage: storage });
const Router = Express.Router();
Router.route("/createAnnouncement").post(
   upload.single("files"),
  (req , resp, next) => {
console.log(" >>>>>>>", req.file);//THis returns undefined
console.log(">>>>", req.files);//This returns undefined
resp.send("Siccess")
 }
);

Below is my Req snapshot Req Snapshot

Its not getting uploaded to storage and its returning undefined when called req.file

But if I try to see req.body I could see [Object File] in console

PLease suggest where I am going wrong

Rookie007
  • 1,229
  • 2
  • 18
  • 50
  • [link](https://stackoverflow.com/questions/65862484/how-to-upload-multiple-images-at-a-time-in-s3-bucket-using-node-js-express/65863294#65863294) here is an example of uploading multiple images with multer, try maybe, router.post("/", upload.single("file"), (req, res) => { const {file} = req; console.log(file); res.send("saved"); } – Erykj97 Jan 26 '21 at 11:24

1 Answers1

0

I think this may help https://github.com/RUSHI-GADHIYA/uploading-and-updating-image see this