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

481
Visualizações
Node.js - Display image from MongoDB

I managed to save image to mongoDB with type Buffer. Together with the image is the username of the user currently logged in. MY problem now is to display the image(s) to the client's side. Much appreciated if it has a condition like "display image where username = 'currentUser_username'". I also have an 'uploads' folder where the images are saved. Please help. I'm a newbie in nodeJS. Please be very detailed in ur answer. Thanks

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

To send the image to the client, you need to save image binary data, image content type.

The structure of your schema should be like this.

var demoSchema = new mongoose.Schema({
    imageData : { type: Buffer },
    imageType : {type: String },
    imageName : {type: String }
})

mongoose.model('Demo', demoSchema)

On saving your image to DB, save the content type too (must).

var data = fs.readFileSync(incomingImage.path);
var input = new Demo():
input.image = new MongoDb.Binary(data);
input.imageType = incomingImage.type;
input.imageName = incomingImage.name;

input.save();

When you retrieve from DB and send the contentType and mention the buffer as binary to the client.

Demo.findById(req.params.id, function (error, result) {
    res.contentType(result.imageType);    
    res.end(result.image.buffer, "binary");
});

If you need some brief detail see here

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