How to save Docx file with multer.diskStorage

Viewed 29

Using : NODEJS, EXPRESS.

So here is the context: I created a form which then transforms the responses into docx with a template (with docx-templater in react).

Then I store my received docx in my mongodb database (works fine).

But here it is, I would also like to store it in an internal folder on the server side.

So I wanted to use multer.

When I "post" my form

  • The docx is saved on my computer
  • The docx is saved on my mongodb

But he is not save in my 'public' folder.

I have no error in console.

Here is my controller

router.post(
  "/upload",
  uploadMiddleware,
  uploadMulter.single("myFile"),
  async (req, res) => {
    console.log(req.file);
    const { file } = req;
    const { id } = file;
    try {
      if (file.size > 5000000) {
        deleteDocx(id);
        return res.status(400).send("file may not exceed 5mb");
      }
    } catch (err) {
      return res.status(201).json(err);
    }

    return res.send(file.id);
  }
);

This is uploadMulter

const multerStorage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, `./public/docx`);
  },
  filename: (req, file, cb) => {
    cb(null, file.filename + "." + file.mimetype);
  },
});

const uploadMulter = multer({
  storage: multerStorage,
});

I've tried

  • To use fileSystem with createWriteStream
  • To change the path with '/public/docx' or ${__dirname}/public/docx
  • I've created my public and docx folder so it's not a folder problem.
0 Answers
Related