2017-12-02 180 views
0

Heyy偷看,我已经在angular2中使用ngx-uploader,作为回应,我发送的文件名是在后端生成的。响应仅在文件完全上传时发送。所以,我怎么可以跟踪这是由事件发射器看起来像这样完成上传过程:EventEmitters如何跟踪上传进度?价值变化?

files: UploadFile[]; 
    uploadInput: EventEmitter<UploadInput>; 

     startUpload(): void { 
     const event: UploadInput = { 
      type: 'uploadFile', 
      url: 'http://localhost:3030/upload/quality-docs', 
      method: 'POST', 
      file: this.files[this.files.length - 1], 
     }; 


     this.uploadInput.emit(event); 
     } 

里面event.file的是进度百分比值,我怎么听/跟踪。所以当它100,做一些事情(得到价值的名字作为回应)?

回答

0

一段时间,我发现这样做..在docs“自述”正道之后有方法调用onUploadOutput(输出:UploadOutput):void {}在那里你只需要添加另一个if语句,看起来像

else if(output.type === 'done') { whatever u wanna do} 
0

我知道这是不是最好的或者正确的方式,但是这是我如何做它的工作:

startUpload(): void { 
    this.uploaded = false; 
    const event: UploadInput = { 
     type: 'uploadFile', 
     url: 'http://localhost:3030/upload/quality-docs', 
     method: 'POST', 
     file: this.files[this.files.length - 1], 
    }; 

    this.uploadInput.emit(event); 
    this.repeat(event); 
    } 

    repeat(event) { 
    setTimeout(() => { 
     this.fileName = event.file.response.filename; 
     if (this.fileName == '' || this.fileName == undefined) { 
     this.repeat(event); 
     } 
     this.uploaded = true; 
    }, 200) 
    }