仅在快速js中验证图像大小后才上传图像

时间:2017-04-04 12:41:33

标签: javascript node.js express file-upload

这是我的代码,用于检查符合条件的图像大小和宽度

im.identify(req.files.image,function (err,features) {      
   //console.log(features);                                
    if(features.width<1000){                               
        console.log('need bigger size');                   
    }                                                      
  });  
 //upload code here
 beginUpload();

由于异步调用beginUpload()函数,所以如何同步检查图像大小。 即我想上传通过大小标准的图像

2 个答案:

答案 0 :(得分:1)

在承诺中调用您的函数。

当你的identify()函数完成后,promise(then())将被执行。

试试这个:

im.identify(req.files.image,function (err,features) {      
    //console.log(features);                                
    if(features.width<1000){                   
        console.log('need bigger size');
        //stop the function to don't execute then()
        return false;                 
    }                                                   
}).then(function() {
    //upload code here
    beginUpload();
});

答案 1 :(得分:0)

@Maitre Manuel正确地指出UI可以实现Promise以确保在发布之前进行异步验证。

This SO question讨论了一些可用于检查本身的代码。例如,在发布之前确定客户端的图像宽度。