2017-12-02 109 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) 
    }