如何使用 angular 图片上传显示图片缩略图
How to display image thumbnail using angular image upload
我正在使用 ng2-file-upload 上传图片。一切正常,但在我选择图片后,它会显示所选图片的缩略图。
请查看我的 stackblitz Link
提前致谢
你可以看看这个demo可能对你有帮助
模板文件:
img
元素显示图像预览
<img [src]="previewImg" *ngIf="previewImg"/>
Class 文件:
URL.createObjectURL()
是一个静态方法,它创建一个包含 URL 的 DOMString,表示参数中给定的对象。
bypassSecurityTrustUrl
绕过安全性并相信给定的值是安全样式 URL,即可以在超链接或 <img src>
.
中使用的值
constructor(private sanitizer: DomSanitizer) {} // inject the DomSanitizer
previewImg: SafeUrl;
this.uploader.onAfterAddingFile = (file) => {
console.log('***** onAfterAddingFile ******')
this.previewImg = this.sanitizer.bypassSecurityTrustUrl((window.URL.createObjectURL(file._file)));;
}
别忘了包括 import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
我得到了解决方案,
我已经在你的 https://stackblitz.com/edit/angular-ng2-file-upload 上测试过了,试试看它是否有效
export class AppComponent implements OnInit {
url = 'https://evening-anchorage-3159.herokuapp.com/api/';
uploader = new FileUploader({
url: this.url,
maxFileSize: 1024 * 1024 * 1
});
name = 'Angular 5';
//added this two variable here
imageUrlOfLogo:string='';
logoFileNameFile?: File;
ngOnInit() {
// your own code here
}
// added this code here
handleLogoFileInput(file: any) {
var item = file.item(0);
this.logoFileNameFile = file.item(0);
var reader = new FileReader();
reader.onload = (event: any) => {
this.imageUrlOfLogo = event.target.result;
}
reader.readAsDataURL(this.logoFileNameFile as File);
}
}
component.html
<p>Maximun allowed file size is 1MB</p>
<img [src]="imageUrlOfLogo" style="width:50%; height:90px;" *ngIf="logoFileNameFile">
<input type="file" ng2FileSelect [uploader]="uploader" (change)="handleLogoFileInput($event.target.files)">
<button (click)="uploader.uploadAll()">Upload </button>
当您的图片准备好后,这里是答案FileReader
显示缩略图
import { Component, OnInit } from '@angular/core';
import {FileUploader} from 'ng2-file-upload';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
url = 'https://evening-anchorage-3159.herokuapp.com/api/';
ready = false;
thumb="";
uploader = new FileUploader({
url: this.url,
maxFileSize: 1024 * 1024 * 1
});
name = 'Angular 5';
ngOnInit() {
this.uploader.onAfterAddingFile = (file) => {
console.log('***** onAfterAddingFile ******')
var image_file=file._file
const reader = new FileReader();
reader.addEventListener('load', () => {
console.log(reader.result)
this.ready=true;
this.thumb=reader.result.toString();
});
reader.readAsDataURL(image_file);
}
this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any) => {
console.log('ImageUpload:uploaded:', item, status, response);
};
this.uploader.onCompleteAll = () => {
console.log('******* onCompleteAll *********')
}
this.uploader.onWhenAddingFileFailed = (item: any, filter: any, options: any) => {
console.log('***** onWhenAddingFileFailed ********')
}
}
}
你的HTML看起来像这样
<hello name="{{ name }}"></hello>
<p>Maximun allowed file size is 1MB</p>
<img [src]="thumb" *ngIf="ready"/>
<input type="file" ng2FileSelect [uploader]="uploader">
<button (click)="uploader.uploadAll()">Upload </button>
我正在使用 ng2-file-upload 上传图片。一切正常,但在我选择图片后,它会显示所选图片的缩略图。
请查看我的 stackblitz Link
提前致谢
你可以看看这个demo可能对你有帮助
模板文件:
img
元素显示图像预览
<img [src]="previewImg" *ngIf="previewImg"/>
Class 文件:
URL.createObjectURL()
是一个静态方法,它创建一个包含 URL 的 DOMString,表示参数中给定的对象。
bypassSecurityTrustUrl
绕过安全性并相信给定的值是安全样式 URL,即可以在超链接或 <img src>
.
constructor(private sanitizer: DomSanitizer) {} // inject the DomSanitizer
previewImg: SafeUrl;
this.uploader.onAfterAddingFile = (file) => {
console.log('***** onAfterAddingFile ******')
this.previewImg = this.sanitizer.bypassSecurityTrustUrl((window.URL.createObjectURL(file._file)));;
}
别忘了包括 import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
我得到了解决方案,
我已经在你的 https://stackblitz.com/edit/angular-ng2-file-upload 上测试过了,试试看它是否有效
export class AppComponent implements OnInit {
url = 'https://evening-anchorage-3159.herokuapp.com/api/';
uploader = new FileUploader({
url: this.url,
maxFileSize: 1024 * 1024 * 1
});
name = 'Angular 5';
//added this two variable here
imageUrlOfLogo:string='';
logoFileNameFile?: File;
ngOnInit() {
// your own code here
}
// added this code here
handleLogoFileInput(file: any) {
var item = file.item(0);
this.logoFileNameFile = file.item(0);
var reader = new FileReader();
reader.onload = (event: any) => {
this.imageUrlOfLogo = event.target.result;
}
reader.readAsDataURL(this.logoFileNameFile as File);
}
}
component.html
<p>Maximun allowed file size is 1MB</p>
<img [src]="imageUrlOfLogo" style="width:50%; height:90px;" *ngIf="logoFileNameFile">
<input type="file" ng2FileSelect [uploader]="uploader" (change)="handleLogoFileInput($event.target.files)">
<button (click)="uploader.uploadAll()">Upload </button>
当您的图片准备好后,这里是答案FileReader
显示缩略图
import { Component, OnInit } from '@angular/core';
import {FileUploader} from 'ng2-file-upload';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
url = 'https://evening-anchorage-3159.herokuapp.com/api/';
ready = false;
thumb="";
uploader = new FileUploader({
url: this.url,
maxFileSize: 1024 * 1024 * 1
});
name = 'Angular 5';
ngOnInit() {
this.uploader.onAfterAddingFile = (file) => {
console.log('***** onAfterAddingFile ******')
var image_file=file._file
const reader = new FileReader();
reader.addEventListener('load', () => {
console.log(reader.result)
this.ready=true;
this.thumb=reader.result.toString();
});
reader.readAsDataURL(image_file);
}
this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any) => {
console.log('ImageUpload:uploaded:', item, status, response);
};
this.uploader.onCompleteAll = () => {
console.log('******* onCompleteAll *********')
}
this.uploader.onWhenAddingFileFailed = (item: any, filter: any, options: any) => {
console.log('***** onWhenAddingFileFailed ********')
}
}
}
你的HTML看起来像这样
<hello name="{{ name }}"></hello>
<p>Maximun allowed file size is 1MB</p>
<img [src]="thumb" *ngIf="ready"/>
<input type="file" ng2FileSelect [uploader]="uploader">
<button (click)="uploader.uploadAll()">Upload </button>