我已经能够使用NodeJS / Express / Amazon S3 / Multer设置一次上传单个图像的过程。它完美地运作。我一直在尝试更改代码,以允许用户一次上传多个图片。到目前为止,我一直很不成功。如何更改下面的代码以允许一次上传多个图像?谢谢!
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;
}......
答案 0 :(得分:2)
你已经完成了困难的部分,所有你需要做的就是修改你的html文件输入,让它接受多个文件,如此
<input type="file" name="img" multiple>
并将数组中的文件数更改为您要上传的文件的最大数量
来自
upload.array('image', 1)
到
upload.array('image', x)
其中(x)是每次上传的最大文件数
EDIT1:更新
这是一个完整的例子&amp;避免“太大的实体问题”
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
});
});
答案 1 :(得分:0)
https://www.npmjs.com/package/multer-s3
您可以使用此npm软件包进行aws-s3上传。我已经使用过相同的文件并保存了文件。