Sails.js with skipper: check if file input were empty before starting upload

旧时模样 提交于 2019-12-11 02:48:35

问题


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:


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.



来源:https://stackoverflow.com/questions/31061719/sails-js-with-skipper-check-if-file-input-were-empty-before-starting-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!