如何在Node.js中使用multer-s3上传多个文件

问题描述

我正在尝试使用Node.js,Express.js和Multer-s3上传多个图像,但是它不起作用。 我有一个称为“程序”的模型,该程序模型具有数组图像属性,但是当我尝试上传多个图像时,我的req.file返回未定义。

这是我的模特

const programSchema = new mongoose.Schema({
  programtype: {
    type: String,required: true,},title: {
   type: String,description: {
    type: String,createdAt: {
    type: Date,default: Date.Now,programImage: {
    type: Array,require: true,});

和我的路线

const Program = require("../models/program");
const fs = require("fs");
const multer = require("multer");
const path = require("path");

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

AWS.config.update({
 secretAccessKey: process.env.S3_SECRECT,accessKeyId: process.env.AWS_ACCESS_KEY,region: process.env.S3_REGION,});

const uploadpath = path.join("public",Program.programImageBasePath);
const imagemineTypes = ["image/jpeg","image/png","image/gif"];
const bucketname = "mybucketname";


s3 = new AWS.S3();
const upload = multer({
 storage: multerS3({
   s3: s3,acl: "public-read",bucket: bucketname,s3BucketEndpoint: true,endpoint: "http://" + bucketname + ".s3.amazonaws.com",key: function (req,file,cb) {
    const uploadpathWithOriginalName = uploadpath + "/" + file.originalname;
    cb(null,uploadpathWithOriginalName);
  },}),});


router.post("/create",upload.array("cover",10),async (req,res,next) => {
console.log(req.file);

const program = new Program({
  programtype: req.body.programtype,title: req.body.title,description: req.body.description,programImage: req.file.location,});
try {
  const programs = await program.save();
  res.redirect("/programs");
} catch {
  if (program.programImage != null) {
    removeprogramImage(program.programImage);
  }
  res.render("programs/new");
 }
});

和我的观点

<h2 style="padding-top: 90px;" > New Programs</h2>
<form action="/programs/create" method="POST" enctype="multipart/form-data">
  <div>
   <label>Image</label>
   <input type="file" name="cover" multiple />
  </div>
  <a href="/programs">Cacel</a>
   <button type="submit">Create</button>
</form>

解决方法

您可以参考此示例。

const s3 = new AWS.S3({
    accessKeyId: 'xxxxxxxxx',secretAccessKey: 'xxxxxxxxx'
});
  const uploadS3 = multer({

    storage: multerS3({
        s3: s3,acl: 'public-read',bucket: 'xxxxxxxx',metadata: (req,file,callBack) => {
            callBack(null,{ fieldName: file.fieldname })
        },key: (req,callBack) => {
            var fullPath = 'products/' + file.originalname;//If you want to save into a folder concat de name of the folder to the path
            callBack(null,fullPath)
        }
    }),limits: { fileSize: 2000000 },// In bytes: 2000000 bytes = 2 MB
    fileFilter: function (req,cb) {
        checkFileType(file,cb);
    }
}).array('photos',10);


exports.uploadProductsImages = async (req,res) => {


    uploadS3(req,res,(error) => {
        console.log('files',req.files);
        if (error) {
            console.log('errors',error);
            res.status(500).json({
                status: 'fail',error: error
            });
        } else {
            // If File not found
            if (req.files === undefined) {
                console.log('uploadProductsImages Error: No File Selected!');
                res.status(500).json({
                    status: 'fail',message: 'Error: No File Selected'
                });
            } else {
                // If Success
                let fileArray = req.files,fileLocation;
                const images = [];
                for (let i = 0; i < fileArray.length; i++) {
                    fileLocation = fileArray[i].location;
                    console.log('filenm',fileLocation);
                    images.push(fileLocation)
                }
                // Save the file name into database
                return res.status(200).json({
                    status: 'ok',filesArray: fileArray,locationArray: images
                });

            }
        }
    })
};