I'm working on a project in angular2, which involves loading an image from a database directly (base64 encoded). In angular1, one could simply load it as follows:
我正在angular2中的一个项目上工作,该项目涉及从数据库直接加载图像(base64编码)。在angular1中,只需按以下方式加载:
<img data-ng-src="data:image/jpg;base64,{{entry.img}}" />
where entry.img is the raw image data. However, I have no idea how to do this in angular2. I've tried
入口的地方。img是原始图像数据。然而,我不知道如何在angular2做这个。我试过了
<img [src]="data:image/jpg;base64,{{entry.img}}" />
but that doesn't work, as angular still tries to load the image from a URL.
但这并不奏效,因为角仍然试图从URL加载图像。
2 个解决方案
#1
17
Perhaps you could try this:
也许你可以试试这个:
<img [src]="'data:image/jpg;base64,'+entry.img" />
assuming that entry
is a property of the component and contains an img
attribute.
假设条目是组件的属性,并且包含img属性。
#2
2
I have used <img [attr.src]="image.base64">
where my model looks like this
我已经使用了
export class ImageModel {
public alt:string = "";
public name:string = "";
public base64:string ='';
}
, but the version of @Thierry works also. I am attaching a part of the component and a piece of html. Maybe you can improve your solution or improve mine :)
,但@Thierry的版本也适用。我附加了组件的一部分和html的一部分。也许你可以改进你的解决方案或者改进我的:)
HTML
HTML
<div class="row">
<div class="col-xs-12">
<p class="f-500 c-black m-b-20">Image Preview</p>
<div class="fileinput fileinput-new" [ngClass]="{'fileinput-exists': fileExists(), 'fileinput-new': !fileExists()}">
<div class="fileinput-preview thumbnail">
<img *ngIf="fileExists()" [attr.src]="image.base64">
</div>
<div>
<span class="btn btn-info btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" #fileInp (change)="onFileChange($event)" name="file">
</span>
<a href="#" class="btn btn-danger fileinput-exists" (click)="clearImage($event)">Remove</a>
</div>
</div>
</div>
</div>
Component
组件
///<reference path="../../../../../typings/tsd.d.ts"/>
import {Component, OnInit} from 'angular2/core'
import {Router} from 'angular2/router';
import {NgClass} from 'angular2/common';
import {ImageInterface} from './image.interface'
import {ImageService} from './image.service'
import {ImageModel as Image} from './image.model'
import {ImageComponent} from './image.component'
@Component({
selector: 'create-images-component',
templateUrl: './angular2/app/images/image-form.component.html',
providers: [
ImageService
],
directives: [
ErrorsComponent,
NgClass,
] })
export class CreateImageComponent implements OnInit {
public image:ImageInterface = new Image();
public validationErrors;
constructor(public service:ImageService,
private _router:Router) {
}
/**
* File input change event
* @param event
*/
onFileChange(event) {
let file = event.target.files[0];
if (!file) {
return;
}
if ( this.isValidExtension(file) == false ) {
swal('Invalid file format!', 'Invalid file Format. Only ' + ImageComponent.ALLOWED_EXTENSIONS.join(', ') + ' are allowed.', 'error');
return false;
}
if (this.isValidFileSize(file) == false) {
swal('Invalid file size!', 'Invalid file size. Max allowed is : ' + ImageComponent.ALLOWED_SIZE + ', your file is : ' + this.getFileSize(file) + ' Mb' , 'error');
return;
}
console.log(file);
let reader = new FileReader();
reader.onload = (e) => {
this.image.base64 = e.target.result;
};
reader.readAsDataURL(file);
}
/**
* Check if file has valid extension
* @param file
*/
private isValidExtension(file:File):boolean {
let allowedExtensions = ImageComponent.ALLOWED_EXTENSIONS;
let extension = file.name.split('.').pop();
return allowedExtensions.indexOf(extension) !== -1
}
/**
* Check file size
* @param file
* @returns {boolean}
*/
private isValidFileSize(file) {
let size = this.getFileSize(file);
return size < ImageComponent.ALLOWED_SIZE;
}
/**
* GEt file size in Mb
* @param file
* @returns {Number}
*/
private getFileSize(file:File):Number {
return file.size / 1024 / 1024;
}
/**
* Clear selected image
* @param ev
*/
clearImage(ev) {
ev.preventDefault();
this.image.base64 = ''
}
/**
* Return if image exists
* @returns {boolean}
*/
fileExists() {
return this.image.base64 && (typeof this.image.base64 !== 'undefined')
}
/**
* Create new image
*/
onSubmit(form) {
return this.service.httpPost(this.image)
.subscribe(
(res) => {
if (res.data.errors) {
this.validationErrors = res.data.errors;
}
if (res.status == "success") {
this.successMessage = res.message;
swal("Good Job", res.message, "success");
this.validationErrors = [];
} else {
this.errorMessage = res.message;
}
},
(error) => this.errorMessage = <any>error);
}
onCancel() {
return this._router.navigate(['Images']);
}
get diagnostic() {
return JSON.stringify(this.image);
}
}
#1
17
Perhaps you could try this:
也许你可以试试这个:
<img [src]="'data:image/jpg;base64,'+entry.img" />
assuming that entry
is a property of the component and contains an img
attribute.
假设条目是组件的属性,并且包含img属性。
#2
2
I have used <img [attr.src]="image.base64">
where my model looks like this
我已经使用了
export class ImageModel {
public alt:string = "";
public name:string = "";
public base64:string ='';
}
, but the version of @Thierry works also. I am attaching a part of the component and a piece of html. Maybe you can improve your solution or improve mine :)
,但@Thierry的版本也适用。我附加了组件的一部分和html的一部分。也许你可以改进你的解决方案或者改进我的:)
HTML
HTML
<div class="row">
<div class="col-xs-12">
<p class="f-500 c-black m-b-20">Image Preview</p>
<div class="fileinput fileinput-new" [ngClass]="{'fileinput-exists': fileExists(), 'fileinput-new': !fileExists()}">
<div class="fileinput-preview thumbnail">
<img *ngIf="fileExists()" [attr.src]="image.base64">
</div>
<div>
<span class="btn btn-info btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" #fileInp (change)="onFileChange($event)" name="file">
</span>
<a href="#" class="btn btn-danger fileinput-exists" (click)="clearImage($event)">Remove</a>
</div>
</div>
</div>
</div>
Component
组件
///<reference path="../../../../../typings/tsd.d.ts"/>
import {Component, OnInit} from 'angular2/core'
import {Router} from 'angular2/router';
import {NgClass} from 'angular2/common';
import {ImageInterface} from './image.interface'
import {ImageService} from './image.service'
import {ImageModel as Image} from './image.model'
import {ImageComponent} from './image.component'
@Component({
selector: 'create-images-component',
templateUrl: './angular2/app/images/image-form.component.html',
providers: [
ImageService
],
directives: [
ErrorsComponent,
NgClass,
] })
export class CreateImageComponent implements OnInit {
public image:ImageInterface = new Image();
public validationErrors;
constructor(public service:ImageService,
private _router:Router) {
}
/**
* File input change event
* @param event
*/
onFileChange(event) {
let file = event.target.files[0];
if (!file) {
return;
}
if ( this.isValidExtension(file) == false ) {
swal('Invalid file format!', 'Invalid file Format. Only ' + ImageComponent.ALLOWED_EXTENSIONS.join(', ') + ' are allowed.', 'error');
return false;
}
if (this.isValidFileSize(file) == false) {
swal('Invalid file size!', 'Invalid file size. Max allowed is : ' + ImageComponent.ALLOWED_SIZE + ', your file is : ' + this.getFileSize(file) + ' Mb' , 'error');
return;
}
console.log(file);
let reader = new FileReader();
reader.onload = (e) => {
this.image.base64 = e.target.result;
};
reader.readAsDataURL(file);
}
/**
* Check if file has valid extension
* @param file
*/
private isValidExtension(file:File):boolean {
let allowedExtensions = ImageComponent.ALLOWED_EXTENSIONS;
let extension = file.name.split('.').pop();
return allowedExtensions.indexOf(extension) !== -1
}
/**
* Check file size
* @param file
* @returns {boolean}
*/
private isValidFileSize(file) {
let size = this.getFileSize(file);
return size < ImageComponent.ALLOWED_SIZE;
}
/**
* GEt file size in Mb
* @param file
* @returns {Number}
*/
private getFileSize(file:File):Number {
return file.size / 1024 / 1024;
}
/**
* Clear selected image
* @param ev
*/
clearImage(ev) {
ev.preventDefault();
this.image.base64 = ''
}
/**
* Return if image exists
* @returns {boolean}
*/
fileExists() {
return this.image.base64 && (typeof this.image.base64 !== 'undefined')
}
/**
* Create new image
*/
onSubmit(form) {
return this.service.httpPost(this.image)
.subscribe(
(res) => {
if (res.data.errors) {
this.validationErrors = res.data.errors;
}
if (res.status == "success") {
this.successMessage = res.message;
swal("Good Job", res.message, "success");
this.validationErrors = [];
} else {
this.errorMessage = res.message;
}
},
(error) => this.errorMessage = <any>error);
}
onCancel() {
return this._router.navigate(['Images']);
}
get diagnostic() {
return JSON.stringify(this.image);
}
}