Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Upload the image only after validating the image size in express js

Here is my code to check the image size and width that matches the criteria

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

Since its asynchronous the beginUpload() function is invoked early so how can i check the image size synchronously. ie i want to upload the image that pass the size criteria

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Call your function in a promise.

When your identify() function is done, the promise (then()) will be execute.

Try this :

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();
});
about 4 years ago · Santiago Trujillo Report

0

@Maitre Manuel correctly notes that the UI can implement a Promise to ensure the asynchronous validation happens before posting.

This SO question discusses some of the code you can use for the check itself. Eg, to determine image width on the client side before posting.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!