Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Agabala's avatar

How to uploading images with Dropzone on nodejs nuxt 3

utils/fileUpload.js

import multer from "multer";
import path from "path";

const storage = multer.diskStorage({
   destination: (req, file, cb) => {
      cb(null, 'uploads');
   },
   filename: (req, file, cb) => {
      cb(null, Date.now() + path.extname(file.originalname));
      // cb(null, `${new Date().toISOString().replace(/:/g, '-')}${file.originalname}`);
   }
});

const upload = multer({
   storage,
   fileFilter: (req, file, cb) => {
      if(file.mimetype === 'image/png' || file.mimetype === 'image/jpg' || file.mimetype === 'image/jpeg' || file.mimetype === 'image/gif') {
         cb(null, true);     
      }else {
         cb(new Error('MimeType not supported'), false);
      }
   },
//    limits: {
//       fileSize: 1024 * 1024 * 5
//   },
});

export default upload;

bookRoute.js

router.post('/upload', authMiddleware.authenticateUser, upload.single('image'), bookController.uploadFile);

bookController.js

const uploadFile = async (req, res) => {
   try {
      if (!req.file) {
         return res.status(400).json({ error: 'No file uploaded' });
      }

      const filePath = req.file.path;
      res.status(200).json({ message: 'File uploaded successfully', filePath });
   } catch (error) {
      console.error('Error uploading file', error);
      res.status(500).json({ error: 'Internal Server ERROR' });
   }
};

DashboardBooks.vue

The photo does not load in the console File uploaded successfully {message: 'File uploaded successfully', filePath: 'uploads/1725613201618.jpg'}

console.log('Uploaded file:', req.file);
console.log('Image path:', image || 'No file');
Server listening on 5000
Uploaded file: undefined
Image path: No file
0 likes
0 replies

Please or to participate in this conversation.