에 대한 몇 가지 이상한 이유가 없을 수 있습니다 또는 튜토리얼 코드 샘플이 온라인을 사용하는 방법을 보여주는 Angular2 민감하는 형식으로 더 이상 간단한 입력을 선택하거나 드롭다운.
내가 필요한 양식을 작성하는 사용자가 선택됩니다. (이미지 파일)
다음과 같은지 않't work. (즉,아바타 시설을 보여주지 않았 모든 값을 변경합니다.)
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>
프로필입니다.구성 요소입니다.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);
}
나는'm 는 조금 늦었지만,이는 누군가를 위해 올 수 있습니다 여기를 찾고 동일한 솔루션 이 내파일 입 접근과 함께 사용할 수 있는 반응성 또는 템플릿 중심의 형태입니다. 데여기.
이's 몇몇 선택적 유효성 검증을 제공하는 데 사용할 수 있는 확인 이미지 크기와 파일 크기,확장,유형은 기본적으로 사용되지 않습니다.
npm 나는 파일 입력-접근자
과 추가 모듈을 AppModule imports:
import {BrowserModule} from '@angular/platform-browser';
import {FileInputAccessorModule} from "file-input-accessor";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FileInputAccessorModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
다음을 사용하여 파일을 입력 같은 다른 어떤 입력:
<!--Reactive Forms-->
<input type="file" multiple [formControl]="someFileControl" />
<input type="file" multiple formControlName="someFileControl" />
<!--Template-driven-->
<input type="file" name="file-input" multiple [(ngModel)]="fileList" />
구독하실 수 있습 valueChanges 재산과 같이 기타 반응성 제어합니다.
당신이 사용할 수 있는 아래 방법 이미지를 업로드하기 위해서 모든 유형의 형태이다.
노출 하나를 변경하는 방법입니다.
<input class="form-control" type="file" name="avatar" (change)="imageUpload($event)">
<img [src]="imageUrl" />
추가 아래 논리에서 당신의 클래스입니다.
// Declare the variable.
imageUrl: any;
//method definition in your class
imageUpload(e) {
let reader = new FileReader();
//get the selected file from event
let file = e.target.files[0];
reader.onloadend = () => {
//Assign the result to variable for setting the src of image element
this.imageUrl = reader.result;
}
reader.readAsDataURL(file);
}
}
이미지를 업로드한 사용할 수 있습니다다.imageUrl를 업데이트 형태로 모델이다. 에 대한 이미지를 업로드하거나 파일을 서버로 취할 수 있 참조 아래에서 링크가 있습니다.
https://stackoverflow.com/questions/35985347/how-to-upload-file-in-angular2?rq=1
알려주는 경우에는 이 솔루션은 당신을 위해 노력하고 있습니다.