Angular 9 "Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'. "
Angular 9 "Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'. "
我在上传图片之前尝试显示所选图片时遇到了这个问题
company.component.ts
handleFileInput(file: File){
console.log(file);
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = event => {
this.imageUrl = event.target.result;
console.log(this.imageUrl);
};
}
HTML
<input
type="file"
#fileInput
(change)="handleFileInput(fileInput.files[0])"
/>
<img [src]="imageUrl" height="50px" alt="">
以下是如何实施的示例:
分量:
public imageUrl : SafeResourceUrl;
constructor(private sanitizer: DomSanitizer) { }
public upload(list: FileList): void {
const urlToBlob = window.URL.createObjectURL(list.item(0))
this.imageUrl = this.sanitizer.bypassSecurityTrustResourceUrl(urlToBlob);
}
模板:
<input type="file" (change)="upload($event.target.files)">
<img [src]="url">
试试吧。
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
constructor(
private dtr: ChangeDetectorRef,
){
}
handleFileInput(event) {
if (event.target.files && event.target.files[0]) {
this.imageFile = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(event.target.files[0]);
reader.onload = () => {
this.url = reader.result;
this.dtr.detectChanges();
};
}
在 Html 文件中。
<input type="file" accept=".png, .jpg, .jpeg" (change)="handleFileInput($event)">
<img [src]="url">
我在上传图片之前尝试显示所选图片时遇到了这个问题
company.component.ts
handleFileInput(file: File){
console.log(file);
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = event => {
this.imageUrl = event.target.result;
console.log(this.imageUrl);
};
}
HTML
<input
type="file"
#fileInput
(change)="handleFileInput(fileInput.files[0])"
/>
<img [src]="imageUrl" height="50px" alt="">
以下是如何实施的示例:
分量:
public imageUrl : SafeResourceUrl;
constructor(private sanitizer: DomSanitizer) { }
public upload(list: FileList): void {
const urlToBlob = window.URL.createObjectURL(list.item(0))
this.imageUrl = this.sanitizer.bypassSecurityTrustResourceUrl(urlToBlob);
}
模板:
<input type="file" (change)="upload($event.target.files)">
<img [src]="url">
试试吧。
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
constructor(
private dtr: ChangeDetectorRef,
){
}
handleFileInput(event) {
if (event.target.files && event.target.files[0]) {
this.imageFile = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(event.target.files[0]);
reader.onload = () => {
this.url = reader.result;
this.dtr.detectChanges();
};
}
在 Html 文件中。
<input type="file" accept=".png, .jpg, .jpeg" (change)="handleFileInput($event)">
<img [src]="url">