使用multerS3和NodeJs上传多个文件

问题描述

我已经尝试了好几天,但似乎无法弄清楚我要去哪里。我的代码适用于本地文件夹中的单个文件上传,但是即使我分别在multer和控制器功能中使用upload.array()req.files也似乎无法上传多个文件。而当我尝试在s3存储桶中上传单个/多个文件时,根本没有任何上传。在下面,我还将提供我的模型结构,以备不时之需。

:: UPDATE :: 文件正在s3存储桶中上传,但是数据库无法捕获文件路径。 imagePath在数据库内部为空白

route.js:

const ProductController = require('./../controllers/productController');
const appConfig = require("./../config/appConfig");
const multer = require('multer');
const aws = require('aws-sdk');
const multerS3 = require('multer-s3');
const path = require('path');
const s3 = new aws.S3({ accessKeyId: "***",secretAccessKey: "***" });
 var upload = multer({
    storage: multerS3({
      s3: s3,bucket: 'mean-ecom',metadata: function (req,file,cb) {
        cb(null,{fieldName: file.originalname + path.extname(file.fieldname)});
      },key: function (req,Date.now().toString())}})});
let setRouter = (app) => {
    let baseUrl = appConfig.apiVersion;
app.post(baseUrl+'/post-product',upload.single('imagePath'),ProductController.postProduct)}
   module.exports = {
     setRouter: setRouter}

ProductController.postProduct:

const ProductModel = mongoose.model('Product')

let postProduct = (req,res) => {
    let newProduct = new ProductModel({

                imagePath: req.file && req.file.path})
            newProduct.save((err,result) => {
                if (err) { console.log('Error at saving new Product :: ProductController',err);
                 res.send(err);
            } else { 
                console.log('Successfully saved new Product'); res.send(result) }
            })}

productModel.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let productSchema = new Schema(
    {
       imagePath: {
                type: String,default: ''    }})
mongoose.model('Product',productSchema);

解决方法

您需要做的第一件事是将模型的imagePath属性从一个字符串更改为一个字符串数组,以便可以存储多个图像

const Schema = mongoose.Schema;
let productSchema = new Schema(
    {
       imagePath: {type: Array}
    })
mongoose.model('Product',productSchema)

然后,在路由器中,您应该使用upload.any()upload.array()应该以相同的方式工作)。 最后,如果要将文件位置存储在模型数组中,则循环抛出文件以更新模型。

let postProduct = (req,res) => {
    // We create a temporal array with the files locations
    let files = [];
    
    // We loop throw the req.files array and store their locations in the temporal files array
    for (let i = 0; i < req.files.length; i++) {
      let file = req.files[i].location
      files.push(file)
    }

    let newProduct = new ProductModel({
            imagePath: files}) //We create the model with the temp array

    newProduct.save((err,result) => {
      if (err) {
        console.log('Error at saving new Product :: ProductController',err);
        res.send(err);
      }
      else{
        console.log('Successfully saved new Product');
        res.send(result)
      }
    })

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...