Sails.js with skipper: check if file input were em

2019-07-14 13:49发布

I'm using Sails 11.1 and skipper 0.5.5. I have a form to create an article in my app. An image can be attached to an article, so there is an input file.

I would like to check, server-side, if this input file was empty, before trying to start upload.

Is it possible, and if yes, how can I do this?

I tried to check req.files, or req.files.photo ("photo" is the input name) and req.body.photo, but they are always undefined, even if there is a file. req.file('photo') is always defined, so I can't check it.

Just a note: when the input is filled with a file, the upload works.

Thanks in advance.

PS: this question can also be found here, on the skipper GitHub issues.

1条回答
冷血范
2楼-- · 2019-07-14 14:27

The only working solution I found while working on this a few weeks back was to upload the file(s) and then see if any file had been uploaded.

    req.file(inputname).upload({
        dirname: targetLocation,
        maxBytes: 2*1024*1024
    },
    function (err, uploadedFiles) {
        // do stuff with uploadedFiles
    });

I'm not sure why your req.file(inputname) returns null even when a file is uploaded.

Importantly, note that calling req.file() is tricky. I had used it to determine the file size, extension and so on initially. Seemed like a good way to avoid uploading anything at all if it were irrelevant. However, if you don't follow that call with an upload() call in 4.5 seconds (default), Node crashes with an ETIMEDOUT error. I had a hard time figuring that out and haven't complained about the above method ever since.

查看更多
登录 后发表回答