多文件上传 - 如何从路由中的多文件中获取值?
multer file upload - how to get a value from multer in route?
我正在使用 multer 和 Express 上传文件。
我想从路由内的 multer storage
对象访问一个值。
我该怎么做?
多重配置(目前我只知道怎么登录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}`;
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;
你可以简单地添加 req.key = keyValue
然后您可以使用 req.key name
在下一条路线中访问
或者您也可以在路由
中访问req.file或req.files对象
在express中一切都是中间件所以你可以很容易地在下一个中间件中传递和访问
这是 Tarique Akhtar Ansari 已经说过的例子。将您的密钥添加到请求对象,以便您可以像这样在 controller/route 中访问 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;
我正在使用 multer 和 Express 上传文件。
我想从路由内的 multer storage
对象访问一个值。
我该怎么做?
多重配置(目前我只知道怎么登录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}`;
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;
你可以简单地添加 req.key = keyValue 然后您可以使用 req.key name
在下一条路线中访问或者您也可以在路由
中访问req.file或req.files对象在express中一切都是中间件所以你可以很容易地在下一个中间件中传递和访问
这是 Tarique Akhtar Ansari 已经说过的例子。将您的密钥添加到请求对象,以便您可以像这样在 controller/route 中访问 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;