当文件被拒绝时,使用multer投掷错误上传多个文件上传

发布于 2025-01-30 17:27:31 字数 2137 浏览 2 评论 0原文

我正在使用.fields上传两个图像([{name:'centerripimage',maxcount:1},{name:'reskaldProofImage',maxcount:1}]);,但是当FileFilter拒绝文件

错误

  if (this.req.files[placeholder.fieldname].length === 1) {
                                                ^

TypeError: Cannot read properties of undefined (reading 'length')

multer投掷错误时,在文件appender.js中的第49行时,它会丢弃错误。 req.files [占位符。fieldname]显示未定义。

case 'OBJECT':
      if (this.req.files[placeholder.fieldname].length === 1) {
        delete this.req.files[placeholder.fieldname]
      } else {
        arrayRemove(this.req.files[placeholder.fieldname], placeholder)
      }
      break

我的代码

const multer  = require('multer');
const path = require('path');

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      console.log('type',req.body.type);
      cb(null, "uploads/images/");
    },
    filename: function (req, file, cb) {
      cb(
        null,
        file.fieldname + "-" + Date.now() + path.extname(file.originalname)
      );
    },
  });


const maxSize = 1 * 1024 * 1024; // for 1MB

var upload = multer({
    storage: storage,
    fileFilter: async (req, file, cb) => {

      var ext = path.extname(file.originalname);
      console.log("ext", ext);

      let validImageExtensions = ['png', 'jpg', 'jpeg'];

      if (validImageExtensions.indexOf(ext.substring(1)) === -1) {
        cb(null, false);
        console.log("invalid extension");
        return cb(new Error("Only " + validImageExtensions + " are allowed with maxsize 1MB"));
      }else{
        console.log("valid extension");
        cb(null, true);
      }
    },
    limits: { fileSize: maxSize },
  });

module.exports = upload;

如果文件扩展名匹配和图像存储在上传文件夹中,则一切正常,但是当扩展不匹配时,会丢弃相同的错误。

我为您创建了GitHub存储库,以更好地测试错误。 https://github.com/alexadvent/multer-multer-multer-multer-multiple-multiple-sissue

并发送put put put put put在URL Localhost请求:运行服务后8000/上传图像。

I am uploading two image using .fields([{ name: 'certificateImage', maxCount: 1 }, { name: 'addressProofImage', maxCount: 1 }]); but it is throwing error when filefilter reject the file

error

  if (this.req.files[placeholder.fieldname].length === 1) {
                                                ^

TypeError: Cannot read properties of undefined (reading 'length')

Multer throw error at line 49 in file-appender.js. req.files[placeholder.fieldname] is showing undefined.

case 'OBJECT':
      if (this.req.files[placeholder.fieldname].length === 1) {
        delete this.req.files[placeholder.fieldname]
      } else {
        arrayRemove(this.req.files[placeholder.fieldname], placeholder)
      }
      break

My code

const multer  = require('multer');
const path = require('path');

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      console.log('type',req.body.type);
      cb(null, "uploads/images/");
    },
    filename: function (req, file, cb) {
      cb(
        null,
        file.fieldname + "-" + Date.now() + path.extname(file.originalname)
      );
    },
  });


const maxSize = 1 * 1024 * 1024; // for 1MB

var upload = multer({
    storage: storage,
    fileFilter: async (req, file, cb) => {

      var ext = path.extname(file.originalname);
      console.log("ext", ext);

      let validImageExtensions = ['png', 'jpg', 'jpeg'];

      if (validImageExtensions.indexOf(ext.substring(1)) === -1) {
        cb(null, false);
        console.log("invalid extension");
        return cb(new Error("Only " + validImageExtensions + " are allowed with maxsize 1MB"));
      }else{
        console.log("valid extension");
        cb(null, true);
      }
    },
    limits: { fileSize: maxSize },
  });

module.exports = upload;

everything work fine if file extension match and image store in upload folder but throw same error when extension doesnt not match.

I have created github repo for the you to better to test error.
https://github.com/AlexAdvent/multer-multiple-issue

and send put request at url localhost:8000/upload-image after running serve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文