最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ng2-file-upload single upload override file upload - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Dec 21, 2019 at 19:01 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Dec 6, 2017 at 15:39 Phạm Quốc BảoPhạm Quốc Bảo 8942 gold badges10 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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./valor-software/ng2-file-upload/issues/703

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

评论列表(0)

  1. 暂无评论