这是我用于上传表单和数据的代码段。我可以将表单数据发布到api,但不能发布到文件。
admin.component.ts
private onFileChange(event: any) {
// let reader = new FileReader();
console.log("in file change method");
if (event.target.files && event.target.files.length > 0) {
this.file = event.target.files[0];
console.log(event.target.files);
}
}
private onSubmit(userform: any) {
let _formData= new FormData();
console.log(userform.value);
userform.value.IdpMetadataFile = this.file;
console.log(userform.IdpMetadataFile)
console.log('formData', userform.value);
this._adminServices.saveAdminConfig(userform.value).subscribe(response => {
console.log(response);
this.saveAdmin = true;
});
console.log("save admin " + this.saveAdmin);
}
.... admin.component.html ...
<div class="form-group">
<p>Browse IDP Metadata file</p>
<input type="file" name="IdpMetadataFile " id="IdpMetadataFile " (change)="onFileChange($event)" [(ngModel)]="ViewAdminConfigData.IdpMetadataFile "
#idpMetaFileName="ngModel">
<br>
答案 0 :(得分:0)
您必须将文件附加到formdata对象
onSubmit(){
let _formData = new FormData();
_formData.append('file', this.file);
// call api to send the data
}