[英]Angular2 - Display image
I created a Angular2 app that allows the user to upload images. 我创建了一个Angular2应用,该应用允许用户上传图像。 I want to implement a preview option.
我想实现预览选项。 However, when i try to imperilment it the image doesn't show up.
但是,当我尝试使图像失真时,图像不会显示。 How do i achieve this feature?
如何实现此功能?
UploadComponent.ts UploadComponent.ts
import * as ng from '@angular/core';
//import { UPLOAD_DIRECTIVES } from 'ng2-uploader';
import {UploadService} from '../services/upload.service';
@ng.Component({
selector: 'my-upload',
providers:[UploadService],
template: require('./upload.html')
})
export class UploadComponent {
progress:any;
logo:any;
filesToUpload: Array<File>;
constructor(public us:UploadService){
this.filesToUpload = [];
}
upload() {
this.us.makeFileRequest("http://localhost:5000/api/SampleData/Upload", this.filesToUpload)
.then((result) => {
console.log(result);
}, (error) => {
console.error(error);
});
}
onFileChange(fileInput: any){
this.logo = fileInput.target.files[0];
}
}
Upload.html Upload.html
<h2>Upload</h2>
<input type="file" (change)="onFileChange($event)" placeholder="Upload image..." />
<button type="button" (click)="upload()">Upload</button>
<img [src]="logo" alt="Preivew">
The way you try it, you don't get the image URL with fileInput.target.files[0]
, but an object. 尝试的方式不是通过
fileInput.target.files[0]
获取图像URL,而是通过对象获取。
To get an image URL, you can use FileReader
( documentation here ) 要获取图像URL,可以使用
FileReader
( 此处的文档 )
onFileChange(fileInput: any){
this.logo = fileInput.target.files[0];
let reader = new FileReader();
reader.onload = (e: any) => {
this.logo = e.target.result;
}
reader.readAsDataURL(fileInput.target.files[0]);
}
filesToUpload: Array<File> = []; url: any; image: any; //file change event filechange(fileInput: any) { this.filesToUpload = <Array<File>>fileInput.target.files; this.image = fileInput.target.files[0]['name']; this.readurl_file(event); } //read url of the file readurl_file(event) { if (event.target.files && event.target.files[0]) { const reader = new FileReader(); reader.onload = (eve: any) => { this.url = eve.target.result; }; reader.readAsDataURL(event.target.files[0]); } }
<div class="form-group"> <label for="image">Image</label> <input type="file" class="form-control" (change)="filechange($event)" placeholder="Upload file..." > </div> <div class="container"> <img [src]="url"> </div>
Using FileReader is not a good practice. 使用FileReader并不是一个好习惯。 If an image is too large it can crash your browser because onload function load whole image in RAM.
如果图像太大,可能会导致浏览器崩溃,因为onload函数会将整个图像加载到RAM中。
Better approach is to use: 更好的方法是使用:
url = URL.createObjectURL($event.target.files[0]);
Then, show it out with DomSanitizer: 然后,使用DomSanitizer展示出来:
this.sanitizer.bypassSecurityTrustUrl(url)
So in ts: 所以在ts中:
constructor(private sanitizer: DomSanitizer) {}
onFileChange(fileInput: any){
this.url = URL.createObjectURL($event.target.files[0]);
}
get previewUrl(): SafeUrl {
return this.sanitizer.bypassSecurityTrustUrl(this.url);
}
And in html: 并在html中:
<img [src]="previewUrl"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.