0

Error: ENOENT: no such file or directory, open 'C:\Users\tanvir\OneDrive\Desktop\Projects\inventory-management\middlewares\images\2023-06-17T15:54:49.898Z-code1.png'

using the package: multer

middelware/uploader.js
const multer = require("multer");
const path = require("path");
const sanitizeFilename = require("sanitize-filename");

const destinationPath = path.resolve(__dirname, "images/");
const storage = multer.diskStorage({
  destination: (req, file, cb) => cb(null, destinationPath),
  filename: (req, file, cb) => {
    const originalFileName = sanitizeFilename(file.originalname);
    const fileName = new Date().toISOString() + "-" + originalFileName;
    cb(null, fileName);
  },
});

const uploader = multer({
  storage,
  fileFilter: (req, file, cb) => {
    const supportedExtension = /png|jpg|jpeg/;
    const extension = path.extname(file.originalname);

    if (supportedExtension.test(extension)) {
      cb(null, true);
    } else {
      cb(new Error("File must be in PNG, JPG, or JPEG format."));
    }
  },
  limits: {
    fileSize: 5000000,
  },
});

module.exports = uploader;

I have already west 3 hours on this problem, but I can't find any solution.

  • Does this answer your question? [Error writing a file using 'fs' in Node](https://stackoverflow.com/questions/44814341/error-writing-a-file-using-fs-in-node) – traynor Jun 17 '23 at 19:44

0 Answers0