小编Cha*_*hne的帖子

如何将 reader.result 转换为字符串?

当我尝试在角度三上上传图像时,下面的打字稿文件中的 reader.result 出现错误,我该如何解决此问题?我console.log(image)在 onImagePicked 函数中添加了它,但它也没有显示在控制台中,为什么它不显示在控制台中?

打字稿文件

     imagePreview:string;


ngOnInit(){
  this.form = new FormGroup({
    title : new FormControl(null,{validators:[Validators.required]}),
    content: new FormControl(null,{validators:[Validators.required]} ),
    image: new FormControl(null, {validators: [Validators.required]})
  });


        onImagePicked(event: Event){
          const file = (event.target as HTMLInputElement).files[0];
          this.form.patchValue({image: file});
          this.form.get('image').updateValueAndValidity();
          console.log(file);
          const reader = new FileReader();
          reader.onload = () => {
            this.imagePreview = reader.result;
          };
          reader.readAsDataURL(file);
        }
Run Code Online (Sandbox Code Playgroud)

网页文件

<mat-card>
  <mat-spinner *ngIf="isLoading"></mat-spinner>
  <form [formGroup]="form" (submit)="onAddPost()"  *ngIf="!isLoading">
    <mat-form-field>
      <input matInput type="text" formControlName="title" placeholder="title"  >
      <mat-error *ngIf="form.get('title').invalid" >Please enter the Title</mat-error>
    </mat-form-field>

    <mat-form-field>
      <textarea …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular-forms angular7

0
推荐指数
1
解决办法
7993
查看次数

标签 统计

angular ×1

angular-forms ×1

angular7 ×1

typescript ×1