Display an image from database using web api in angularjs
How to retrieve and display an image from the database based on employee_id after clicking on the search button using web api in angularjs in asp.net in visual studio 2013
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanwar RanwaRank #23 · 44.7K reputationPosted Mar 21, 2019, 11:59 PM EDT
P.Suresh Kumar51 reputationPosted Mar 22, 2019, 12:05 AM EDT
Sanwar RanwaRank #23 · 44.7K reputationPosted Mar 22, 2019, 12:09 AM EDT
Siddhartha SharmaRank #2,254 · 626 reputationPosted Mar 22, 2019, 9:40 PM EDT
[Route("api/profileimage/{userId}")]
public string Get(string userId)
{
var image = _profileImageService.GetProfileImage(userId);
return System.Convert.ToBase64String(image);
}
Angular code :import {Directive, OnInit, Input} from '@angular/core';
import {Http} from '@angular/http';
import {BROWSER_SANITIZATION_PROVIDERS, DomSanitizationService} from '@angular/platform-browser';
@Directive({
selector: '[profile-image]',
providers: [BROWSER_SANITIZATION_PROVIDERS],
host: {
'[src]': 'sanitizedImageData'
}
})
export class ProfileImageDirective implements OnInit {
imageData: any;
sanitizedImageData: any;
@Input('profile-image') profileId: number;
constructor(private http: Http,
private sanitizer: DomSanitizationService) { }
ngOnInit() {
this.http.get("http://localhost:2116/api/ProfileImage/" + profileId)
.map(image => image.text())
.subscribe(
data => {
this.imageData = 'data:image/png;base64,' + data;
this.sanitzedImageData = sanitizer.bypassSecurityTrustUrl(imageData);
}
);
}
}