问题描述
我已经能够建立一个使用NodeJS / Express / Amazon S3 / Multer一次上传单个图像的流程。它完美的作品。我一直在尝试更改代码,以允许用户一次上传多个图像。到目前为止,我一直非常失败。我如何更改我的代码以允许多个图像一次上传?谢谢!$ $ p $
aws.config.update({
secretAccessKey:'********** *******',
accessKeyId:'******',
region:'us-east-2'
});
var s3 = new aws.S3();
$上传= multer({
存储:multerS3({
s3:s3,
存储桶:'myfiles',
键:function(req,file,cb){
var fileExtension = file.originalname.split(。)[1];
var path =uploads /+ req.user._id + Date .now()+。+ fileExtension;
cb(null,path);
}
})
});
$ b router.post(/,upload.array('image',1),function(req,res,next){
var filepath =未定义;
if(req.files [0]){
filepath = req.files [0] .key;
} ......
修改您的html文件输入,使其接受多个文件,如:
< input type =filename =img multiple>
并将阵列中的文件数量更改为最大文件数上传
from
upload.array('image',1 )
到
upload.array('image',x)
每次上传文件的最大数量
EDIT1:更新
完整的例子和避免太大 e实体问题
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
});
//如果你想要c请求的URL
router.use(function(req,res,next){
console.log('Request URL:', req.originalUrl);
next();
});
//将文件上传到文件夹名称上传,html文件输入名称上传文件
app.post('/ your / route',urlencodedParser,function(req,res){
$ b $ 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){
//上传文件
});
});
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;
}......
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
from
upload.array('image', 1)
to
upload.array('image', x)
where (x) is the maximum number of files per upload
EDIT1 : update
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&亚马逊S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!