2017-03-14 54 views
2

我正在將文件傳輸到網址,我可以成功地做到這一點,但我不知道如何獲取已上傳文件的進度,我需要在某些方面取得進展數字如何在上傳文件時添加進度條

fileTransfer.upload(file_path, api_endpoint, options, data) 
           .then((data) => { 
            // success 
            console.log("success", JSON.parse(data['response'])); 

            this.upload_success(); 

           }, (err) => { 
            // error 
            this.failed_upload(); 
            console.log('File failed uploaded.', err); 
           }) 

我發現onProgress(監聽)check ionic2文檔我不知道如何使用可能有人給我一些例子

更新 通過文檔會經過https://github.com/apache/cordova-plugin-file-transfer#example-with-upload-headers-and-progress-events-android-and-ios-only

fileTransfer.onProgress = function(progressEvent) { 
    if (progressEvent) { 
     console.log("progress success =====") 
    } else { 
     console.log("progress error =====") 
    } 
};  

如果我運行,我看不到任何這些控制檯,我在文件傳輸代碼下面添加了這個新代碼。可有一個人幫我

+0

有[在文檔]爲例(https://github.com/apache/cordova-plugin-file-transfer#example-with-upload-headers-and-progress-events-android-and-ios-only),它使用'onprogress'方法 – Und3rTow

+0

你告訴我它是如何工作的以及將它放在上面的代碼中@ Und3rTow –

回答

2

可以顯示進度條使用onProgress功能這樣

public fileTransfer: TransferObject = this.transfer.create(); 
public upload(path,folder) 
{ 
    url="http://www.axmag.com/download/pdfurl-guide.pdf"; 
    var trustHosts = true; 
    this.presentLoading("Loading"); 
    //**Progress Bar** 
    this.fileTransfer.onProgress((e)=> 
    { 
    let prg=(e.lengthComputable) ? Math.round(e.loaded/e.total * 100) : -1; 
    console.log("progress:"+prg); 
    }); 

    this.fileTransfer.upload(file_path, api_endpoint, options, data).then((entry) => { 
//handle success   
console.log('upload complete: '); 
    }, 
    (error) => { 
    this.loader.dismiss(); 
    alert('uploading faild: '); 
    console.log(error); 
    //handle error 
    }); 
} 

現在你可以調用上傳()函數來上傳文件