Search code examples
node.jssails.jsskippersails-skipper

Sails JS file upload; how to check if file is an input?


I am using Sails JS v11.05.

The file upload is NOT a required field.

How do I check if the file was uploaded or not?

Usually, I use

if (!req.param('param')) {
   // param does NOT exist - was not uploaded
   // This does not apply to file input params.
}

// However, the following will break as it will start a listener (upstream)
if (!req.file('param')) {
   // File was not uploaded
}

All I want to know whether a file is an input or not so I don't bother to call req.file('file').upload() if it wasn't uploaded.

Ideas?


Solution

  • I couldn't find anyway to do this but we can use the Upstream listener to upload an empty file but then it will return and empty uploadedFiles in the callback.

    Note that if you run req.file('file_input') you might kill the node server as it invokes a listener.

    For more info see https://www.npmjs.com/package/skipper

    The solution

    req.file('file_input').upload(options, function (err, uploadedFiles) {
            if (err) return cb(err);
            if (_.isEmpty(uploadedFiles)) return cb();
            // A file was attached do 
    });