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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Siddhartha SharmaPosted Mar 23, 2019, 1:40 AM
[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);
}
);
}
}
Sanwar RanwaPosted Mar 22, 2019, 4:09 AM
P.Suresh KumarPosted Mar 22, 2019, 4:05 AM
Sanwar RanwaPosted Mar 22, 2019, 3:59 AM