10

As the title, now I can not check file type before upload. I only verify and don't allow save data after file uploaded successfully. Below is a 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 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 correct code? Sorry for my bad English! Thanks a lot!

Thanh Dao
  • 1,218
  • 2
  • 16
  • 43

1 Answers1

6

This took me time to research on, all credits to Darkstra who gave an idea of where we can get the raw file properties, like the content-type or even the file name, where we can split to get the file extension and do our checkings

you can check out his answer here

The main thing is to take note of this

req.file('foo')._files[0].stream which contains everything we need to handle our file, you can console.log it to see the content of it.

At least you can do any manipulation of your choice.

Theophilus Omoregbee
  • 2,463
  • 1
  • 22
  • 33
  • problem is that after calling req.file('foo') without uploading cause my app to crash Error: EMAXBUFFER – Kreator Jun 02 '20 at 08:32