javascript - How can I validate file type before upload use SailsJS Skipper -
as title, can not check file type before upload. verify , don't allow save data after file uploaded successfully. below basic 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 uploaded!'); if (allowexts.indexof(files[0].type) == -1) return res.badrequest('file type not supported!'); // save database here }); }
what should correct code? sorry bad english! lot!
this took me time research on, credits darkstra gave idea of can raw file properties, content-type or file name, can split file extension , our checkings
you can check out answer here
the main thing take note of this
req.file('foo')._files[0].stream
contains need handle our file, can console.log see content of it.
thanks, atleast can manipulation of choice.
Comments
Post a Comment