简体   繁体   English

ng2-file-upload 单次上传覆盖文件上传

[英]ng2-file-upload single upload override file upload

I do the demo base on this tutorial: https://github.com/valor-software/ng2-file-upload .我根据本教程进行演示: https : //github.com/valor-software/ng2-file-upload I want to single file upload but without remove file button.我想上传单个文件但没有删除文件按钮。 By adding the File A, after that I add the File B. File A will be replaced by file B. here is my uploader:通过添加文件 A,然后我添加文件 B。文件 A 将被文件 B 替换。这是我的上传者:

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

Please advice me请给我建议

As Arun Muthiyarkath suggested, you can use the onAfterAddingFile , but the shorter code would be:正如 Arun Muthiyarkath 建议的那样,您可以使用onAfterAddingFile ,但较短的代码是:

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

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

Probably you can use onAfterAddingFile callback of the library provided function.可能您可以使用库提供的函数的onAfterAddingFile回调。 Below is the sample code.下面是示例代码。 This is always override old file with latest file and queue will always contain one item which is the latest file.这总是用最新文件覆盖旧文件,队列将始终包含一项,即最新文件。

  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);
} 
this.uploader.onAfterAddingFile = (fileItem: FileItem) => {
  if (this.uploader.queue.length > 0) {
    this.uploader.queue = [fileItem];
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM