3

我根据本教程进行演示:https ://github.com/valor-software/ng2-file-upload 。我想上传单个文件但没有删除文件按钮。通过添加文件 A,然后我添加文件 B。文件 A 将被文件 B 替换。这是我的上传器:

 this.uploader = new FileUploader(
      {
        url: this.baseURL,
        allowedFileType: ["xls"],
        maxFileSize: 5,
        queueLimit: 1
      });

请给我建议

4

3 回答 3

3

正如 Arun Muthiyarkath 建议的那样,您可以使用onAfterAddingFile,但较短的代码将是:

  ngOnInit() {
    this.uploader.onAfterAddingFile = (fileItem: FileItem) => {
      if (this.uploader.queue.length > 1) {
        this.uploader.removeFromQueue(this.uploader.queue[0]);
      }
    };
  }

来源:https ://github.com/valor-software/ng2-file-upload/issues/703

于 2019-09-01T06:58:22.917 回答
2

可能您可以使用onAfterAddingFile库提供的函数的回调。下面是示例代码。这总是用最新文件覆盖旧文件,并且队列将始终包含一项最新文件。

  ngOnInit() {

     this.uploader.onAfterAddingFile = (fileItem: FileItem) => this.onAfterAddingFile(fileItem)

}

onAfterAddingFile(fileItem: FileItem) {
   let latestFile = this.uploader.queue[this.uploader.queue.length-1]
   this.uploader.queue = []; 
   this.uploader.queue.push(latestFile);
} 
于 2018-08-02T05:53:12.137 回答
0
this.uploader.onAfterAddingFile = (fileItem: FileItem) => {
  if (this.uploader.queue.length > 0) {
    this.uploader.queue = [fileItem];
  }
};
于 2019-12-21T18:17:28.057 回答