【问题标题】:ng2-file-upload single upload override file uploadng2-file-upload 单次上传覆盖文件上传
【发布时间】:2018-05-20 13:24:06
【问题描述】:

我根据本教程进行演示: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
      });

请给我建议

【问题讨论】:

    标签: javascript angular file-upload ng2-file-upload


    【解决方案1】:

    正如 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

    【讨论】:

      【解决方案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);
      } 
      

      【讨论】:

        【解决方案3】:
        this.uploader.onAfterAddingFile = (fileItem: FileItem) => {
          if (this.uploader.queue.length > 0) {
            this.uploader.queue = [fileItem];
          }
        };
        

        【讨论】:

        • 请不要单独发布代码作为答案。尝试添加更多上下文,说明为什么这会解决发布的原始问题。
        猜你喜欢
        • 2020-01-15
        • 2020-01-07
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 2016-09-18
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        相关资源
        最近更新 更多