I have got a little function running with formidable to accept incoming files. It works like a charm, but i cannot see anywhere in the docs about restricting file types. https://github.com/felixge/node-formidable It seems almost everything else is covered except this.
Has anyone else come across this?
var form = new formidable.IncomingForm(),
files = [],
fields = [],
returnJson = {};
//setup the incoming
form.uploadDir = GLOBAL.server_settings.user_content;
form.encoding = 'utf-8';
form.maxFieldsSize = 2 * 1024 * 1024;
form.maxFields = 1000;
form.on('field', function(field, value) {
console.log(field, value);
fields.push([field, value]);
})
/* this is where the renaming happens */
.on ('fileBegin', function(name, file){
var fileType = file.type.split('/').pop();
//rename the incoming file
file.path = form.uploadDir + "/" + req.user.id + _ + toolbox.uniqid() + '.' + fileType;
})
.on('file', function(field, file) {
//on file received
console.log(field, file);
files.push([field, file]);
})
.on('progress', function(bytesReceived, bytesExpected) {
//self.emit('progess', bytesReceived, bytesExpected)
var percent = (bytesReceived / bytesExpected * 100) | 0;
process.stdout.write('Uploading: %' + percent + '\r');
})
.on('end', function() {
console.log('-> upload done');
console.log( files );
console.log( fields );
returnJson.file_data = files;
returnJson.fields_data = fields;
res.json( returnJson );
});
form.parse(req);