字節(jié)跳動小程序API downloadFile

2019-08-15 11:12 更新

下載網(wǎng)絡文件到本地臨時目錄。

提示

網(wǎng)絡相關的 API 在使用前需要配置域名白名單。請參考網(wǎng)絡請求使用說明

提示

HTTP 請求 method 為GET

輸入


繼承標準對象輸入,擴展屬性描述:

名稱 數(shù)據(jù)類型 屬性 默認值 描述
url string required N/A 文件地址
header object optional `` 請求Header

輸出


success返回對象參數(shù)的擴展屬性:

名稱 數(shù)據(jù)類型 描述
tempFilePath string 文件本地路徑
statusCode number 返回 HTTP 狀態(tài)碼

本 API 的調用結果在通過回調傳遞的同時會返回一個downloadTask對象,可以通過該對象的abort方法中斷請求任務;通過onProgressUpdate方法監(jiān)聽下載進度。

onProgressUpdate注冊的監(jiān)聽方法會收到一個object類型參數(shù),該參數(shù)的屬性值有:

名稱 數(shù)據(jù)類型 描述
progress number 下載進度
totalBytesWritten number 已經(jīng)下載的數(shù)據(jù)長度,單位byte
totalBytesExpectedToWrite number 預期需要下載的數(shù)據(jù)總長度,單位byte

代碼示例


let task = tt.downloadFile({
    url: 'somefileurl',
    success (res) {
        if (res.statusCode === 200) {
            console.log(`${res.tempFilePath}`);
        }
    },
    fail (res) {
        console.log(`downloadFile調用失敗`);
    }
});

task.onProgressUpdate((res) => {
    this.setData({
        progress: res.progress
    });
});

if (someReason) {
    task.abort();
}

已知問題



以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號