我的对象"uploadData"不显示传递给它的信息,而只是在控制台上显示"FormData{}"



我想上传一个文件,因此创建一个新对象,我想将其传递给名为"uploadData"的服务器。我想从使用角度表单构建器创建的"uploadForm"对象在此对象中添加值。但在控制台上,它只显示"FormData{}"。

const uploadData = new FormData();
        this.uploadData.append("name", this.uploadForm.value.name);
        this.uploadData.append("sem", this.uploadForm.value.sem);
        this.uploadData.append("subject", this.uploadForm.value.subject);
        this.uploadData.append("material", this.uploadForm.value.material);
        this.uploadData.append("id", this.userId);
        this.uploadData.append("pdf",this.uploadForm.value.pdf, this.uploadForm.value.name);
        //this.updown.upload(this.uploadData).subscribe()
        console.log(this.uploadData);

表单生成器是

uploadForm = this.fb.group({
name: ['', Validators.required],
sem: ['', Validators.required],
subject: ['', Validators.required],
material: ['', Validators.required],
pdf: ['', Validators.required]
});

Pdf 是一个文件,因此:

onImagePick(event: Event){
const file = (event.target as HTMLInputElement).files[0];
this.uploadForm.patchValue({ pdf : file });
this.uploadForm.get('pdf').updateValueAndValidity();
}

您使用this.updown.upload(this.uploadData)但将其声明为 const uploadData。删除此关键字,您应该没问题。

对于另一个问题,FormDatas是不可变的,这意味着append返回一个新的表单数据实例。

尝试改用此代码:

const uploadData = new FormData()
    .append("name", this.uploadForm.value.name)
    .append("sem", this.uploadForm.value.sem)
    .append("subject", this.uploadForm.value.subject)
    .append("material", this.uploadForm.value.material)
    .append("id", this.userId)
    .append("pdf",this.uploadForm.value.pdf, this.uploadForm.value.name);
    //this.updown.upload(this.uploadData).subscribe()
    console.log(this.uploadData);

相关内容

  • 没有找到相关文章

最新更新