簡體   English   中英

如何在 Nodejs 中使用 multer-s3 上傳多個文件

[英]How to upload multiple files with multer-s3 in Nodejs

我正在嘗試使用 Nodejs、Expressjs 和 Multer-s3 上傳多個圖像,但它不起作用。 我有一個名為 Program 的 model 和 Program model 有一個數組圖像屬性,但是當我嘗試上傳多個圖像時,我的req.file返回未定義。

這是我的 model

const programSchema = new mongoose.Schema({
  programtype: {
    type: String,
    required: true,
  },
  title: {
   type: String,
   required: true,
  },
  description: {
    type: String,
    required: true,
  },
  createdAt: {
    type: Date,
    required: true,
    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, file, 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, file, 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
                });

            }
        }
    })
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM