简体   繁体   English

上传多张图片时合并丢失的文件

[英]Multer missing files when uploading multiple images

I am trying to upload multiple images with multer but it only uploads some images not all of them.我正在尝试使用 multer 上传多张图片,但它只上传一些图片而不是全部。 Eg if I try 10 images only 5-6 gets uploaded without error.例如,如果我尝试 10 张图片,则只有 5-6 张没有错误地上传。 Alos image size is not that big hardly maximum 200-300 kb I am also not getting any error. Alos 图像大小不是那么大,几乎没有最大 200-300 kb 我也没有收到任何错误。 SO no idea how to fix it.所以不知道如何解决它。 Any help would be appreciated.任何帮助,将不胜感激。

const express = require("express");
const router = express.Router();
const multer = require("multer");

const { body } = require("express-validator");
const productController = require("../controller/productController");
const { protect, admin } = require("../middleware/authMiddleware");
const path = require("path");
// const upload = multer();
// const upload = require("../middleware/uploadMiddleware");

const storage = multer.diskStorage({
    destination(req, file, cb) {
        cb(null, "uploads/");
    },
    filename: function (req, file, cb) {
        cb(
            null,
            `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`
        );
    },
});

function checkFileType(file, cb) {
    const fileTypes = /jpg|jpeg|png|svg|webp/;
    const extname = fileTypes.test(path.extname(file.originalname).toLowerCase());
    const mimetype = fileTypes.test(file.mimetype);
    if (extname && mimetype) return cb(null, true);
    else cb("Images only!");
}
const upload = multer({
    storage,
fileFilter: function (req, file, cb) {
        checkFileType(file, cb);
    },
});

/* 
  DETAILS - ADMIN CREATE PRODUCT ROUTE
  METHOD  -  POST
*/
router.post(
    "/",
    upload.array("galleryImages"),
    [
        body("title", "Please enter product name").notEmpty().trim(),
        body("color", "Please enter color").notEmpty().trim(),
        body("weight", "Please enter product weight").notEmpty().trim().isNumeric(),
        body("price", "Please enter product price").notEmpty().trim().isNumeric(),
        body("manufacturer", "Please enter your city").notEmpty().trim(),
        body("sizes", "Please enter your product size").isArray(),
        body("details", "Please enter product details").notEmpty().trim(),
        // body("galleryImages", "Please enter your "),
    ],

    productController.addProduct
);

module.exports = router;

For anyone who is facing this similar issue.对于任何面临类似问题的人。 you can console log console.log(req.files) in your controller and then hit the API.您可以在 controller 中控制台日志 console.log(req.files),然后点击 API。 Date.now() is not that fast and create same name for files which cause this issue. Date.now() 不是那么快,并为导致此问题的文件创建相同的名称。 You can use uniqid https://www.npmjs.com/package/uniqid instead of Date.now() function您可以使用 uniqid https://www.npmjs.com/package/uniqid代替 Date.now() function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM