我在 ionic 2 中创建了一个表单,其中有一个字段可以上传 .docs 或 .pdf 类型的简历。我尝试添加如下,
<form [formGroup]="myForm">
<ion-list>
<ion-item>
<input type="file" formControlName="upresume_one" name="upresume_one"/>
<p>Supported formats .doc,.docs and .pdf[Max file size: 500KB]</p>
</ion-item>
<div class="text-right">
<button ion-button style="background-color: #16a085;color: white;" color="secondary" (click)="save(myForm.value)">Submit</button>
</div>
</ion-list>
</form>
我的 .ts 文件如下:
myForm: FormGroup;
private myData: any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
private builder: FormBuilder,
private userProfileService: UserProfileService,
private progressDialog: ProgressDialog) {
this.myForm = builder.group({
'upresume_one': ['', Validators.required]
})
在提交时,我正在调用如下所示的保存功能,
save(formData) {
console.log('Form data is ', formData);
}
在 consoel.log 中,即使在选择了有效文件后,我仍然为空。有人可以建议我在ionic 2中将输入类型文件集成到表单中的最佳方法是什么。
最佳答案
我终于找到了这个问题的答案。您必须有一个单独的 API 可用于上传文件。以下是在ionic 2中上传文件的详细步骤:
this.fileChooser.open() .then(uri => console.log(uri)) .catch(e => console.log(e));
https://gist.github.com/coolvasanth/b266c5bb382ddbfc60ca1c0f7c9f33c0
关于cordova - ionic 2 : Uploading docs, pdf 到服务器的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43346710/