【发布时间】:2021-02-16 00:42:36
【问题描述】:
我目前正在构建一个文件上传,我可以上传一个或多个 .xml 文件。
<button mat-raised-button class="button" (click)="fileInput.click()"
<span>Choose files</span>
<input #fileInput type="file" multiple accept=".xml" style="display:none;" (change)="onFileInput($event)">
</button>
onFileInput 方法将这些文件推送到一个数组中。
onFileInput($event) {
Array.from($event.target.files).forEach(file => {
this.files.push(file);
})
this.files = [...new Set(this.files)];
}
问题是如果我改变上传文件的数量,我可以多次上传同一个文件。
案例 1:
上传文件 1,2
上传文件 1,2
this.files 包含文件 1,2
到目前为止一切顺利
案例 2:
上传文件 1,2
上传文件 1
this.files 包含 1,2,1
上传文件 1
this.files 仍然包含 1,2,1
上传文件 2
this.files 包含 1,2,1,2
希望您能理解问题并帮助我,因为我找不到我的错误。
提前致谢
【问题讨论】:
-
Set比较 instances(const a = {foo: 'bar'}; new Set([a, a])不是 values 除了基元(字符串、数字等)。您可以通过比较手动完成例如文件名或文件大小。
标签: javascript arrays angular typescript duplicates