Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda