Reputation: 85
I am trying to store image in mongodb and display it back to Angular. I am done with upload, but I am getting error while I try to display. I tested my API in POSTMAN. It is working fine. I think the error is because of the response type.
API
get('/images',(req, res, next) => {
const user = AuthBase.getUser(req, res);
this.model.findOne({\[this.userForeignKey\]: new mongoose.Types.ObjectId(user._id)}, (err, image) => {
if (err) {
res.sendStatus(400);
}
// stream the image back by loading the file
res.setHeader('Content-Type', 'image/jpeg');
fs.createReadStream(path.join(UPLOAD_PATH, image.filename)).pipe(res);
//if I add res.json(image) here, I am not getting any error but the Image is getting broken
})
});][1]
Service
getImage(): Observable<any> {
return this.http.get(`/api/images`);
}
Component
images:[]
getImage()
{
this.patientService.getImage().subscribe(
data =>{
this.images = [data];
console.log(data); },
error => console.log(error) );
}
Upvotes: 1
Views: 2545
Reputation: 27303
Include the response type with get request
getImage(): Observable<any> {
return this.http.get(`/api/images`,{ responseType: 'blob' });
}
Upvotes: 2