【问题标题】:multer file upload - how to get a value from multer in route?multer 文件上传 - 如何在路由中从 multer 获取值?
【发布时间】:2020-09-08 09:47:04
【问题描述】:

我正在使用带有 Express 的 multer 上传文件。

我想从路由内 multer 的 storage 对象访问一个值。
我该怎么做?

Multer 配置(目前我只知道如何记录密钥):

const aws = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");

function configureUpload () {
    
  const s3 = new aws.S3({...my credentials...});

  const upload = multer({
    storage: multerS3({
      s3: s3,
      bucket: process.env.S3_BUCKET_NAME,
      metadata: (req, file, cb) => cb(null, { fieldName: file.fieldname }),
      key: (req, file, cb) => {
        const key = `${new Date().toISOString()}-${file.originalname}`;
        return cb(console.log("KEY: ", key), key); // The string I need to access in route
      },
    }),
  });

  return upload;
}

路线:

const express = require("express");
const Person = require("../../../db/models/person");
const configureUpload = require("../../../configureUpload ");

const router = express.Router();

// Saving to MongoDB with mongoose

router.post("/", configureUpload ().any(), async (req, res) => {
  Person.create({
    ...req.body,
    files: [] // I want to add the string in multer.storage.key to this array
  })
  .then((person) => {
    ...
   })
   .catch((err) => {
    ...
   });

});

module.exports = router;

【问题讨论】:

    标签: express file-upload multer


    【解决方案1】:

    您可以简单地添加 req.key = keyValue 然后您可以使用 req.key 名称在下一条路线中访问

    或者你也可以在路由中访问 req.file 或 req.files 对象

    所有东西都是一个中间件,所以你可以很容易地在下一个中间件中传递和访问

    【讨论】:

      【解决方案2】:

      这是 Tarique Akhtar Ansari 已经说过的话的一个例子。将您的密钥添加到 req 对象,以便您可以访问控制器/路由中的 key 值,如下所示:

      const aws = require("aws-sdk");
       const multer = require("multer");
       const multerS3 = require("multer-s3");
      
      function configureUpload () {
          
        const s3 = new aws.S3({...my credentials...});
      
        const upload = multer({
          storage: multerS3({
            s3: s3,
            bucket: process.env.S3_BUCKET_NAME,
            metadata: (req, file, cb) => {cb(null, { fieldName: file.fieldname })},
            key: (req, file, cb) => {
              
              const key = `${new Date().toISOString()}-${file.originalname}`;
              req.key = key; // added the key to req object
      
              // return cb(console.log("KEY: ", key), key); // The string I need to access in route
            },
          }),
        });
      
        return upload;
      }
      

      访问控制器或路由内部密钥的value

      const express = require("express");
      const Person = require("../../../db/models/person");
      const configureUpload = require("../../../configureUpload ");
      
      const router = express.Router();
      
      // Saving to MongoDB with mongoose
      
      router.post("/", configureUpload ().any(), async (req, res) => {
      
       console.log('here is the value your key', req.key); // it's that simple.
      
        Person.create({
          ...req.body,
          files: [] // I want to add the string in multer.storage.key to this array
        })
        .then((person) => {
          ...
         })
         .catch((err) => {
          ...
         });
      
      });
      
      module.exports = router;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-30
        • 2016-03-19
        • 1970-01-01
        • 2017-08-30
        • 2018-07-21
        • 2018-02-25
        • 2016-06-18
        • 2020-10-30
        相关资源
        最近更新 更多