gpt4 book ai didi

file-upload - 如何在 Angular2 react 形式中包含文件上传控件?

转载 作者:行者123 更新时间:2023-12-03 10:27:49 25 4
gpt4 key购买 nike

出于某种奇怪的原因,没有在线教程或代码示例展示如何使用 Angular2 Reactive 表单,而不仅仅是简单的输入或选择下拉列表。

我需要创建一个表单来让用户选择他们的头像。 (图像文件)

以下不起作用。 (即 Avatar 属性从不显示任何值更改。)

profile.component.html:

               <form [formGroup]="profileForm" novalidate>

<div class="row">
<div class="col-md-4 ">
<img src="{{imgUrl}}uploads/avatars/{{getUserAvatar}}" style="width:150px; height:150px;float:left;border-radius:50%;margin-right:25px;margin-left:10px;">

<div class="form-group">
<label>Update Profile Image</label>
<input class="form-control" type="file" formControlName="avatar">
</div>
</div>
<div class="col-md-8 ">
<div class="form-group">
<label >Firstname:
<input class="form-control" formControlName="firstname">
</label>
</div>
<div class="form-group">
<label >Lastname:
<input class="form-control" formControlName="lastname">
</label>
</div>
<div class="form-group">
<label >Email:
<input class="form-control" formControlName="email">
</label>
</div>
<div class="form-group">
<label >Password:
<input class="form-control" type="password" formControlName="password">
</label>
</div>
</div>
</div>

</form>
<p>Form value: {{ profileForm.value | json }}</p>
<p>Form status: {{ profileForm.status | json }}</p>


profile.component.ts:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {Config} from '../../services/config.service';
import {AuthService} from '../../services/auth.service';
import {User} from '../../models/user.model';

@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {

authUser:User;

profileForm : FormGroup;

constructor(private authService:AuthService, private fb: FormBuilder) {}

createForm() {
this.profileForm = this.fb.group({
firstname: [this.authUser.firstname, Validators.required ],
lastname: [this.authUser.lastname, Validators.required ],
email: [this.authUser.email, Validators.required ],
avatar: [this.authUser.avatar, Validators.required ],
password:['xxxxxx', Validators.minLength(4)]
});
}
ngOnInit() {
this.authUser = this.authService.getAuthUser();

this.createForm();
}

最佳答案

可以在这里找到简单的答案。
https://devblog.dymel.pl/2016/09/02/upload-file-image-angular2-aspnetcore/

HTML

    <input #fileInput type="file"/>
<button (click)="addFile()">Add</button>


组件.ts
@ViewChild("fileInput") fileInput;

addFile(): void {
let fi = this.fileInput.nativeElement;
if (fi.files && fi.files[0]) {
let fileToUpload = fi.files[0];
this.uploadService
.upload(fileToUpload)
.subscribe(res => {
console.log(res);
});
}
}

服务.ts
upload(fileToUpload: any) {
let input = new FormData();
input.append("file", fileToUpload);

return this.http.post("/api/uploadFile", input);
}

关于file-upload - 如何在 Angular2 react 形式中包含文件上传控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43444440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com