I use window.URL.createObjectURL to create a blob:http link for previewing selected image in an img tag:
<img src=""{{itemPhoto}}"" />
itemPhoto is a field defined in a component and gets assigned when an image file is selected:
selectPhoto(photos: any[]) {
if (photos[0]) {
this.itemPhoto = window.URL.createObjectURL(photos[0]);
}
}
This works in angular2 RC1 but no longer works in 2.0.0.
Here is what gets into the src attribute:
unsafe:blob:http://localhost:5555/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
I tried the following after reading some other posts:
this.itemPhoto = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(photos[0]));
Then the following gets into the src attribute:
unsafe:SafeValue must use [property]=binding: blob:http://localhost:5555/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx (see http://g.co/ng/security#xss)
Any suggestions?
Many thanks
Update: OK, I didn't really understand that error message inside src: "unsafe:SafeValue must use [property]=binding:..."
Instead of src={{itemPhoto}}
, the following works:
<img [src]="itemPhoto" />
Still not sure why though.
The URL. createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.
createObjectURL is synchronous but it seems to complete almost instantaneously.
To create a Blob we call new Blob() then call window. URL. createObjectURL() to convert it into a URL.
You could just try what error is trying to say. what it said is you have to use property
[] binding instead of interpolation
{{}} with attribute.
[src]="itemPhoto"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With