【发布时间】:2019-05-31 21:56:22
【问题描述】:
我正在尝试使表单组注册图像。 这是我的文件上传模板:
<label for="file-loader" class="float-right">
<img src="assets/images/robot-02-icon.png" alt="robot" *ngIf="!imgURL">
<img [src]="imgURL" height="200" *ngIf="imgURL">
</label>
<input type="file" formControlName="photo_id" id="file-loader" (change)="readURL($event)">
这是我的 readURL 函数():
readURL(event) {
if (event.target.files.length === 0) {
return;
}
const reader = new FileReader();
this.imagePath = event.target.files;
this.registerForm.patchValue({
photo_id: event.target.files[0]
});
reader.readAsDataURL(event.target.files[0]);
reader.onload = ( ) => {
this.imgURL = reader.result;
console.log(this.imgURL = reader.result);
this.registerForm.patchValue({
photo_id: reader.result
});
};
}
我的表单生成器是这样的:
this.registerForm = this.formBuilder.group({
name: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
password: ['', Validators.required],
photo_id: [''],
});
当我尝试提交表单时,我在控制台中收到此消息:
SingupComponent.html:10 ERROR DOMException: 未能在“HTMLInputElement”上设置“value”属性:此输入元素接受文件名,该文件名只能以编程方式设置为空字符串。
【问题讨论】:
标签: angular