Max*_*fer 6 file-upload angular
我正在尝试在Angular 6中构建一个表单,它提供了一个文件选择框,允许用户选择多个文件(Stackblitz:https://stackblitz.com/edit/angular-yjummp ).
模板看起来像:
<form [formGroup]="form">
<input id="files" formControlName="files" type="file" accept="image/*,video/*" multiple (change)="onFilesChanged($event)"/>
</form>
Run Code Online (Sandbox Code Playgroud)
我在TypeScript中构建表单,如下所示:
public form = new FormGroup({
files: new FormControl('')
});
public onFilesChanged(event: any): void {
console.log(event.target.files);
console.log(this.form.value);
}
Run Code Online (Sandbox Code Playgroud)
现在,在onFilesChangedHandler中,通过访问event.target.files(显然)可以从事件中正确检索所选文件,但打印表单值只打印一个文件.我一直在尝试使用多种方式FormArray,但到目前为止还没有运气.
有任何想法吗?非常感谢!
Jav*_*ies 13
下面是我如何通过角度反应形式实现多张照片上传输入文件的示例。
public demoForm: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.demoForm = this.formBuilder.group({
text_input: ['', Validators.required],
photos: this.formBuilder.array([])
});
}
// We will create multiple form controls inside defined form controls photos.
createItem(data): FormGroup {
return this.formBuilder.group(data);
}
//Help to get all photos controls as form array.
get photos(): FormArray {
return this.demoForm.get('photos') as FormArray;
};
detectFiles(event) {
let files = event.target.files;
if (files) {
for (let file of files) {
let reader = new FileReader();
reader.onload = (e: any) => {
this.photos.push(this.createItem({
file,
url: e.target.result //Base64 string for preview image
}));
}
reader.readAsDataURL(file);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在组件 html 中
<input type="file" class="custom-file-input form-control" id="files" multiple (change)="detectFiles($event)" accept="image/x-png,image/jpeg">
<div class="images-preview mt-2" *ngIf="photos.length">
<div formArrayName="photos" *ngFor="let photo of photos.controls; let i = index;">
<div [formGroupName]="i">
<img [src]="photo.controls.url.value" class="card-img-top" alt="Image Preview">
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如果您不想显示预览,则可以避免使用 html。在提交表单时,您将获得如下值。
{
text_input: "",
photos: [
{
file: <File Object>,
url: <Base64 String here>
},
{
file: <File Object>,
url: <Base64 String here>
},
....
]
}
Run Code Online (Sandbox Code Playgroud)
编辑 这里是工作stackblitz示例
我希望它能帮助你和其他人。谢谢。