正如标题,现在我无法在上传前检查文件类型。我只验证文件上传成功后不允许保存数据。下面是基本代码
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
});
}
我应该怎么做才能获得正确的代码? 对不起,我的英语不好! 非常感谢!
最佳答案
这花了我时间研究,所有功劳归于 Darkstra谁知道我们可以从哪里获取原始文件属性,例如内容类型甚至文件名,我们可以在哪里拆分以获取文件扩展名并进行检查
你可以看看他的回答 here
主要是要注意这一点
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.
至少你可以做任何你选择的操作。
关于javascript - 如何在上传使用 SailsJS Skipper 之前验证文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32303604/