0

I'm building a project in angular 6 and asp.net core. I have an images saved on the server, and I save the image path and image name in the db.

I've tried passing the image path to angular after using domSanitizer but the browser still says "Not allowed to load local resource: {filepath}"

component.ts

photo: Photo;
imageToShow: any;

getPhoto() {
    this.fileService.getPhoto().subscribe(response => {
      this.photo = response;
      this.imageToShow = this.sanitizer.bypassSecurityTrustResourceUrl(this.photo.thumbPhotoPath);
    });
  }

component.html

<img [src]="imageToShow">

service.ts

getPhoto(id: number) {
      return this.http.get<Photo>(this.baseUrl + 'get-photo/' + id);
  }

Controller

[HttpGet("get-photo/{id}")]
public async Task<IActionResult> GetPhoto(int id) 
{
    var photo = await _repo.GetPhoto(id);

    var pathToImage = Path.Combine(photo.ThumbPhotoPath, photo.ImageName);

    FilePhoto photoData = new FilePhoto()
    {   
        Id = photo.Id,
        ThumbPhotoPath = pathToImage,
    };
    return Ok(photoData);
}

Does anyone know the best way to display the image stored on the server to angular? Should I be passing it back as a blob instead? and if so what would be the best way to do that?

Thanks

6
  • return from the API the file to the angular stackoverflow.com/questions/40794275/… Commented May 8, 2019 at 16:45
  • What is the value for photoData? If you access the photoData.ThumbPhotoPath directly from web browser, will it show the image correctly? Share us the generated html for <img [src]="imageToShow">. Where did you save the image? Commented May 9, 2019 at 2:08
  • @TaoZhou the value for photoData is just the photo id and the path to the photo on the server. The image is saved in the wwwroot on the .net core api, and I want to display that image in the angular app. Commented May 9, 2019 at 4:28
  • @TaoZhou The console in the browser has the error "IMAGE PATH: SafeValue must use [property]=binding: C:\Users\{rest of the image path} "and "Not allowed to load local resource: file:///C:/Users/ {the rest of image path}". I was passing that path into the [src] on the image. Commented May 9, 2019 at 4:35
  • @TiagoSilva I'll try this as well. Do you know what the component on the angular side would look like? Commented May 9, 2019 at 4:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.