How to check file type before loading SailsJS Skipper

As the name, now I can not check the file type before downloading. I check and do not allow saving data after a successful file upload. Below is the base code

updateAvatar : function(data, context, req, res) { req.file('avatar').upload({ dirname: '../../assets/images/avatar' }, function (err, files) { var allowExts = ['image/png', 'image/gif', 'image/jpeg']; if (err) return res.serverError(err); if (files.length === 0) return res.badRequest('No file was uploaded!'); if (allowExts.indexOf(files[0].type) == -1) return res.badRequest('File type is not supported!'); // save database here }); } 

What should I do for the correct code? Sorry for my bad english! Thank you very much!

+6
source share
1 answer

It took me a while to research, all the Darkstra credits that gave an idea of ​​where we can get the properties of the raw file, such as the content type or even the file name, where we can split to get the file extension and do our checks

you can check his answer here

The main thing to note is

req.file('foo')._files[0].stream , which contains everything we need to process our file, you can console.log it to see its contents.

Thank you, so at least you can do any kind of manipulation of your choice.

0
source

All Articles