我正在尝试上传不同的文件。但是当我第二次选择一个文件时,问题就出现了,它被添加到 fileArray,它不会被替换。
例如,我为第一个输入选择了一张图片。然后我决定为相同的输入选择不同的图片,然后它将被添加到文件数组中,所以当我上传它时,之前的图片也将被上传,这是我不想上传的。
是否有任何解决方案(除了使用单个按钮上传多个文件),以便当我第二次选择文件时,它将被替换而不是添加到文件数组中?
uploadDocument.html 文件:
<form (ngSubmit)=f.form.valid && formSubmit() #f="ngForm">
<input type="file" class="form-control ml-2" name="photo" ng2FileSelect [uploader]="uploader" />
<input type="file" class="form-control ml-2" name="document1" ng2FileSelect [uploader]="uploader" />
<input type="file" class="form-control ml-2" name="pic2" ng2FileSelect [uploader]="uploader" />
<button type="submit" class="btn btn-danger btn-lg btn-fill">Upload</button>
</form>
uploadDocument.component.ts 文件:
import { Component, OnInit, Renderer, ElementRef } from '@angular/core';
import { UserService } from '../_services';
import { ActivatedRoute, Router, Params} from '@angular/router';
import { FileUploader } from 'ng2-file-upload/ng2-file-upload';
import { FileSelectDirective } from 'ng2-file-upload';
const URL = 'http://localhost:3002/api/upload';
@Component({
selector: 'app-uploadDocument',
templateUrl: './uploadDocument.component.html',
styleUrls: ['./uploadDocument.component.scss']
})
export class UploadDocument implements OnInit {
model: any = {};
mobile: number;
options: boolean;
loading = false;
public uploader:FileUploader = new FileUploader({url: URL});
constructor(private userService: UserService, private route:ActivatedRoute,private router:Router, private el: ElementRef) { }
ngOnInit() {
this.uploader.onBeforeUploadItem = (item)=> {console.log("Item"); console.log(item)};
this.uploader.onAfterAddingFile = (file)=> { file.withCredentials = false;};
this.uploader.onCompleteItem = (item:any, response:any, status:any, headers:any) => {
console.log("ImageUpload:uploaded:", item, status, response);
};
}
formSubmit() {
this.uploader.uploadAll();
}
}