0

Im trying to upload a file with custom name with multer in nodejs

this is how i call from the frontend

const id = *some value*
Axios.post("http://localhost:3001/upload", formData, {headers: {'content-type': 'multipart/form-data'}})
        .then((response) => {
            window.location.reload()
        })

And this in the backend

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, '../client/src/Images')
    },
    filename: (req, file, cb) => {
        cb(null, Math.trunc(Date.now()/1000).toString() + path.extname(file.originalname))
    }
})

const upload = multer({
    storage: storage,
    limits:{fieldSize: 1000000},
 })

app.post("/upload", upload.single('image'), (req, res) => {
    res.send("Image Uploaded")
})

Now the image is (correctly) saved with the name Date.now()/1000; but i want to saved with the name id that i have in the frontend, how can i pass that to multer?

0 Answers0