【发布时间】:2019-03-09 06:51:55
【问题描述】:
我想知道如何使用 angular 和 form builder 上传文件,目前我只找到了使用 formbuilder 的教程和像这样的单个文件 File Upload In Angular?
我的问题是.ts 文件,如何将文件附加到表单生成器中的字段?我已经看到它使用 formData() 完成,但无法让它们一起工作,非常感谢任何提示!
component.ts
ngOnInit() {
this.escolaridad_candidato = this.fb.group({
nivel_estudio: [, [Validators.required]],
escuela: [, [Validators.required]],
graduacion: [, [Validators.required]],
certificado: [, [Validators.required]] <--This is the file I need to append
});
}
onEstudiosChange(event) {
const reader = new FileReader();
if (event.target.files && event.target.files.length) {
const [file] = event.target.files;
reader.readAsDataURL(file);
reader.onload = () => {
this.exp_academica.patchValue({
certificado: reader.result
});
// need to run CD since file load runs outside of zone
this.cd.markForCheck();
};
}
}
// On Submit button
this.myservice.post('myUrl', this.escolaridad_candidato.rawdata(), configOptions )
component.html
<form [formGroup]="escolaridad_candidato" >
<div class="col s6 center-align" >
<p >Estudios</p>
<div class="row">
<div class="col s12">
<p >Degree</p>
<select class="browser-default" formControlName="nivel_estudio">
<option value="" disabled selected>----</option>
<option *ngFor="let nivel of listnivelEstudios" value="{{ nivel.id }}">
{{ nivel.nombre }}
</option>
</select>
</div>
<div class="col s12 center-align input-field">
<p >Escuela </p>
<input id="escuela" type="text" class="validate browser-default" formControlName="escuela"/>
</div>
<div class="col s12 input-field center-align">
<p >Año de graduación </p>
<input id="graduacion" type="text" class="validate browser-default"
formControlName="graduacion" />
</div>
</div>
<div class="row">
<div class="col s12">
<p >Certificate </p>
<div class="file-field input-field">
<div class="btn" >
<span>Adjuntar archivo +</span>
<input type="file" formControlName="certificado" (change)="onEstudiosChange($event)">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</div>
</div>
</div>
</form>
【问题讨论】:
-
你好詹姆。您能告诉我您正在使用的 Angular 版本吗?
-
我正在使用 Angular 7,抱歉耽搁了。你认为这仍然有效吗?
-
我担心我将不得不对那部分进行返工,而我现在就要崩溃了????明天我会用新的解决方案回复。
-
非常感谢您的帮助!
-
杰米随时没问题 :)。
标签: angular multipartform-data form-data formbuilder