naval Hurpade
naval Hurpade

Reputation: 11

How to provide Download Link to Image (JPG) from mongoDB through nodejs

I Have stored Image in MongoDB (Buffer) now I want to use this image in my react native app by url like this

e.g http://localhost:8080/fullImg.jpeg

How can I Do this ?

here is my MongoDB Schema

const listingSchema = mongoose.Schema({
  title: String,
  image:{
      fullImg: { imgBuffer: Buffer, contentType: String },
    },
  price: Number,
})

const Listing = new mongoose.model("Listing", listingSchema);

currently Im sending this Buffer to app and converting it to base64

if posible tell best way to store and retrive the image from mongo db

Upvotes: 1

Views: 453

Answers (1)

NeNaD
NeNaD

Reputation: 20304

The best practice would be to upload image to some storage (Amazon S3, Google Cloud Storage...) and then store the url of the uploaded image to MongoDB. After you fetch data from MongoDB, just set src property of img tag to be url of the uploaded image.

If you want to go with your approach of storing Buffer in the MongoDB, you can display the base64 data in HTML like this:

<img src="data:image/png;base64, {{your_base64_data}}"/>

Upvotes: 1

Related Questions