【发布时间】:2018-12-26 01:36:55
【问题描述】:
我正在学习从链接https://angular.io/guide/reactive-forms 获取表单值。但是那里没有提供文件上传,所以我修改了代码并在表单和组件中添加了文件上传选项,但没有获取路径。我得到的输出是
{
"firstName": "p",
"lastName": "a",
"photo": "C:\\fakepath\\gorgeous-wallpapers-hd-for-desktop-6.jpg"
}
在这我得到不正确的照片路径。
profile.component.ts
import { Component } from '@angular/core';
import { FormBuilder } from '@angular/forms';
@Component({
selector: 'app-profile-editor',
templateUrl: './profile.component.html',
styleUrls: []
})
export class ProfileEditorComponent {
profileForm = this.fb.group({
firstName: [''],
lastName: [''],
photo: ['']
});
constructor(private fb: FormBuilder) {}
public obj: any;
updateProfile() {
this.obj = this.profileForm.value;
}
}
profile.component.html
<p>
Form Status: {{obj|json}}
</p>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<form [formGroup]="profileForm">
<div class="form-group">
<label> First Name:</label>
<input type="text" class="form-control" formControlName="firstName">
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" class="form-control" formControlName="lastName">
</div>
<div class="form-group">
<label>Upload File:</label>
<input type="file" class="form-control" formControlName="photo">
</div>
<div class="form-group">
<button class="btn btn-primary" (click)="updateProfile()">Update Name</button>
</div>
</form>
</div>
</div>
【问题讨论】:
标签: angular