4

I do the demo base on this tutorial: 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:

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

Please advice me

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Phạm Quốc Bảo
  • 864
  • 2
  • 10
  • 19

3 Answers3

4

As Arun Muthiyarkath suggested, you can use the onAfterAddingFile, but the shorter code would be:

  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

Rijaz Ali
  • 225
  • 1
  • 2
  • 10
3

Probably you can use onAfterAddingFile callback of the library provided function. 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);
} 
0
this.uploader.onAfterAddingFile = (fileItem: FileItem) => {
  if (this.uploader.queue.length > 0) {
    this.uploader.queue = [fileItem];
  }
};
Malekai
  • 4,765
  • 5
  • 25
  • 60
hanan
  • 1,768
  • 1
  • 14
  • 19
  • 1
    Please don't post code by itself as an answer. Try to add more context as to why this would solve the original question posted. – mjuarez Dec 22 '19 at 09:43