I\'m trying to develop a module that should upload files from a single html form.
Imagine a form that contains two files:
1) the avatar of the user;
2) the curriculum vi
I resolved myself! This is my not perfect solution (it works well for destination, filename and filetypes but not for maxsize and files) however, I want share my code:
in app.js I create an object like this:
var obj={ destination: {input-name1:'./uploads', input-name:'./uplds'}, filename: { input-name1:'file1', input-name2:'file2'}, FieldNameSize:undefined, fieldSize:undefined, fields:undefined, fileSize:{input-name1:2*1024*1024, input-name2:10*1024*1024}, files:{input-name1:3,input-name2:1}, parts:undefined, filetypes:{input-name1:"jpeg,jpg"} };
and in rules_upload.js:
var path = require('path'); var multer = require('multer'); var defaultMaxSize=3*1024*1024; var defaultFiles=1;
exports.upload = function (obj, next) { var upld = {}; var storage={}; var limits = {}; for (key in obj) { switch (key) { case 'destination': storage.destination= function (request, file, cb) { cb(null,obj.destination[file.fieldname]); }; break; case 'filename': storage.filename=function (request, file, cb) { if(obj.filename[file.fieldname]!=undefined) { nome=obj.filename[file.fieldname]; if(nome==undefined) nome=file.originalname.substring(0, file.originalname.lastIndexOf("."));
nome+=file.originalname.substring(file.originalname.lastIndexOf('.'),file.originalname.length);
} else nome=file.originalname.split('/').pop().trim();
cb(null, nome);
};
break;
case 'FieldNameSize': limits.fieldNameSize = function (request, file, cb) { if(obj.fieldNameSize[file.fieldname]!=undefined) cb(null,obj.fieldNameSize[file.fieldname]); else return cb(null, true); }; break; case 'fieldSize': limits.fieldSize= function (request, file, cb) { if(obj.fieldSize[file.fieldname]!=undefined) cb(null,obj.fieldSize[file.fieldname]); else return cb(null, true); }; break; case 'fields': limits.fields = function (request, file, cb) { if(obj.fields[file.fieldname]!=undefined) cb(null,obj.fields[file.fieldname]); else return cb(null, true); }; break; case 'fileSize': limits.fileSize = function (request, file, cb) { if(obj.fileSize[file.fieldname]!=undefined) cb(null,obj.fileSize[file.fieldname]); else return cb(null, true); }; break; case 'files': limits.files = function (request, file, cb) { if(obj.files[file.fieldname]!=undefined) cb(null,obj.files[file.fieldname]); else return cb(null, true); }; break; case 'parts': limits.parts = function (request, file, cb) { if(obj.parts[file.fieldname]!=undefined) cb(null,obj.parts[file.fieldname]); else return cb(null, true); }; break; case 'filetypes': upld.fileFilter= function (req, file, cb) { if(obj.filetypes[file.fieldname]!=undefined){ var f=obj.filetypes[file.fieldname]; f=f.replace(",","|"); var re= new RegExp(f); var mimetype = re.test(file.mimetype); var extname = re.test(path.extname(file.originalname).toLowerCase()); if (mimetype && extname ) {return cb(null, true); } cb("Sono Accettate solo le seguenti estensioni: " + obj.filetypes[file.fieldname]); } else return cb(null, true); }; break; } } if(storage.filename==undefined) storage.filename= function (request, file, cb) { nome=file.originalname.split('/').pop().trim(); cb(null, Date.now()+"_"+nome); }; if(limits.fileSize==undefined) limits.fileSize=defaultMaxSize; /* var maxSize=l.maxSize; limitsfileSize= function (request, file, cb) { if(file.size>(maxSize)) { cb(null,true); } else { cb("Il file non può pesare più di"+maxSize+" MB" ); } }; */ upld.storage= multer.diskStorage(storage); upld.limits=limits;
return multer(upld).any();
};