上传多张图片 - NodeJS &亚马逊 S3 [英] Upload multiple images - NodeJS & Amazon S3

查看:25
本文介绍了上传多张图片 - NodeJS &亚马逊 S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用 NodeJS/Express/Amazon S3/Multer 设置一次上传单个图像的过程.它完美地工作.我一直在尝试更改代码以允许用户一次上传多个图像.到目前为止,我一直很不成功.我将如何更改下面的代码以允许一次上传多个图像?谢谢!

I have been able to set up a process to upload a single image at a time using NodeJS/Express/Amazon S3/ Multer. It works perfectly. I've been trying to change the code to allow users to upload more than one image at a time. So far I have been very unsuccessful. How would I change my code below to allow multiple images to be uploaded at once? Thanks!

aws.config.update({
    secretAccessKey: '*****************',
    accessKeyId: '******',
    region: 'us-east-2'
});

var s3 = new aws.S3();


var upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'myfiles',
        key: function (req, file, cb) {
            var fileExtension = file.originalname.split(".")[1];
            var path = "uploads/" + req.user._id + Date.now() + "." + fileExtension;
            cb(null, path); 
        }
    })
});


router.post("/", upload.array('image', 1), function(req, res, next){

      var filepath = undefined;

    if(req.files[0]) {
        filepath = req.files[0].key;
    }......

推荐答案

你已经完成了最难的部分,你所要做的就是修改你的 html 文件输入,让它接受多个文件

you have done the hard part, all what u have to do is to modify your html file input to make it accept multiple files like so

<input type="file" name="img" multiple>

并将数组中的文件数更改为您要上传的最大文件数

and change the number of files in the array to the maximum number of files you wan to upload

来自

upload.array('image', 1)

upload.array('image', x)

其中 (x) 是每次上传的最大文件数

where (x) is the maximum number of files per upload

更新

这是一个完整的例子 &避免实体太大问题"

Here is kind of full example & to avoid "too large entity issue"

var express = require("express");
var app = express();

var multer = require('multer');
var cookieParser = require('cookie-parser');
var path = require('path');

var router = express.Router();
app.use("/", router);

app.use(bodyParser.json({limit: "50mb"}));
app.use(cookieParser());
var urlencodedParser = bodyParser.urlencoded({
    extended: true,
    parameterLimit: 50000
});

// in case u want to c the requsted url
router.use(function(req, res, next) { 
    console.log('Request URL: ', req.originalUrl);
    next();
});

//the files will b uploaded to folder name uploads, html file input name is uploadedFile
app.post('/your/route', urlencodedParser, function(req, res) { 

            var storage = multer.diskStorage({
                destination: function(req, file, callback) {
                    callback(null, './uploads');
                },
                filename: function(req, file, callback) {
                    var fname = file.fieldname + '-' + Date.now() + path.extname(file.originalname);
                    callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
                }
            });
            var upload_photos = multer({
                storage: storage
            }).array('uploadedFile', 3);
            upload_photos(req, res, function(err) {
            // uploading files
        });

    });

这篇关于上传多张图片 - NodeJS &amp;amp;亚马逊 S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆