gpt4 book ai didi

angular - 错误 : Property 'split' does not exist on type 'string | ArrayBuffer' . 类型 'split' 上不存在属性 'ArrayBuffer'

转载 作者:太空狗 更新时间:2023-10-29 18:00:37 25 4
gpt4 key购买 nike

当我从 angular 6 项目上传图像时,我正在尝试按照教程将图像转换为 Base64。当我按下提交按钮时,我可以获得作为值的输出:“base64 代码”,而且我还可以通过使用“将你的 Base64 转换为图像”将代码转换为图像来获得相同的图像。但在代码中,它显示“属性‘split’在类型‘string | ArrayBuffer’上不存在。属性‘split’在类型‘ArrayBuffer’上不存在。”用红色下划线分隔错误。我试过 similar question & answers 也。但它无法消除错误。

我的代码是

import {Component, ElementRef, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";

@Component({
selector: 'base64-upload',
templateUrl: './base64-upload.component.html'
})
export class Base64UploadComponent {
form: FormGroup;
loading: boolean = false;


@ViewChild('fileInput') fileInput: ElementRef;

constructor(private fb: FormBuilder) {
this.createForm();
}

createForm() {
this.form = this.fb.group({
name: ['', Validators.required],
avatar: null
});
}

onFileChange(event) {
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
reader.readAsDataURL(file);
reader.onload = () => {
this.form.get('avatar').setValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1]
})
};
}
}

onSubmit() {
const formModel = this.form.value;
this.loading = true;
// this.http.post('apiUrl', formModel)
setTimeout(() => {
console.log(formModel);
alert('done!');
this.loading = false;
}, 1000);
}

clearFile() {
this.form.get('avatar').setValue(null);
this.fileInput.nativeElement.value = '';
}

}

我的html代码是

<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Bob" formControlName="name">
</div>

<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" id="avatar" (change)="onFileChange($event)" #fileInput>
<button type="button" class="btn btn-sm btn-default" (click)="clearFile()">clear file</button>
</div>

<button type="submit" [disabled]="form.invalid || loading"
class="btn btn-success">Submit <i class="fa fa-spinner fa-spin fa-fw" *ngIf="loading"></i>
</button>

</form>

最佳答案

您必须转换要拆分的值。

(<string>reader.result).split(',')[1]

关于angular - 错误 : Property 'split' does not exist on type 'string | ArrayBuffer' . 类型 'split' 上不存在属性 'ArrayBuffer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52032591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com