Mic*_*ski 0 javascript typescript angular
我想添加一个图像文件到“转换”功能。
这是我来自 component.html 的输入代码:
<li>
<label for="avatarIMG" id="avatarLbL"> image: </label>
<input type="file" accept="image/*" #imageBox name="image" id="avatarinput" (change)="convert($event)">
<button type="button" id="avatarInputBTN" (click)="imageBox.click()"> Profile Picture </button>
</li>
Run Code Online (Sandbox Code Playgroud)
该事件假设将对象的值以及所有值+图像文件从表单发送到 component.ts,这是它的代码:
public convert(e: Event): void {
this.eventFiles = (e.target as HTMLInputElement).files[0];
if (this.eventFiles !== null) {
this.user.image = this.eventFiles;
const fileReader = new FileReader();
fileReader.onload = args => this.preview = args.target?.result?.toString();
fileReader.readAsDataURL(this.eventFiles);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,对象可能为 null for (e.target as HTMLInputElement).files[0]。
我怎样才能解决这个问题?..
尝试这个:
this.eventFiles = (e.target as HTMLInputElement)?.files?.[0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3067 次 |
| 最近记录: |