0

I'm working on Angular application and I can not display image on my website, I'm getting this messages in console:

unsafe:url(http://local.api.test.come/Uploads/af21cb1b-066c-48c6-b6d6-0116a133810d.jpg):1

GET unsafe:url(http://local.api.test.com/Uploads/af21cb1b-066c-48c6-b6d6-0116a133810d.jpg) net::ERR_UNKNOWN_URL_SCHEME

If I paste this path into browser I can easily preview image:

local.api.test.come/Uploads/af21cb1b-066c-48c6-b6d6-0116a133810d.jpg

But In my app it wont be loaded..

Here is my code:

<div *ngFor="let content of contents;">
  <div class="card-img-actions mx-1 mt-1">
    <img class="card-img img-fluid" [src]="'url('+ apiPath + content.path +')'" alt={{content.fileName}} />
  </div>
</div>

As It's possible to see URL is correct But I can not load image throught app.. if I enter url in browser as I said it opens image without any issues ...

P.S I've tried using sanitizer like this in my HTML(template):

[src]="domSanitizer.bypassSecurityTrustUrl('url('+ apiPath + content.path +')')"

But than this appends http://localhost:4200 + full url which is absolutly something that I dont want because resouce has nothing with http://localhost:4200

Thanks guys

Cheers

1
  • Why are you using the url() attribute only available in CSS? You should be able to specify the URL directly in the src attribute of the img tag in HTML. Commented Sep 7, 2019 at 18:20

1 Answer 1

1

This is probably because you've mistaken the CSS url() function with the way to specify a URL/path for the src attribute of an img tag. Instead, remove the surrounding url() portion in your code and replace it with the following:

<div *ngFor="let content of contents">
  <div class="card-img-actions mx-1 mt-1">
    <img class="card-img img-fluid" [src]="apiPath + content.path" [alt]="content.fileName" />
  </div>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Also please ensure api path has a scheme else it will be considered as a relative url
@Edric how come Url caused issues ?
Because you're supposed to just specify the URL without the url() appended to it. The url() part is meant for CSS. (I've already specified why it didn't work in my answer)

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.