百度智能小程序 獲取本地已保存的文件列表

2020-09-05 14:21 更新

swan.getSavedFileList

解釋:獲取本地已保存的文件列表

Web 態(tài)說明: 受瀏覽器限制,Web 態(tài)不支持文件系統(tǒng)相關(guān)功能,調(diào)用該方法會執(zhí)行失敗回調(diào)函數(shù)。

方法參數(shù)

Object object

object 參數(shù)說明

屬性名類型必填默認(rèn)值說明

success

Function

接口調(diào)用成功的回調(diào)函數(shù),返回結(jié)果見 success 返回參數(shù)說明。

fail

Function

接口調(diào)用失敗的回調(diào)函數(shù)

complete

Function

接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會執(zhí)行)

success 返回參數(shù)說明

參數(shù)名類型說明

fileList

Array.<object>

文件列表

fileList 中的項(xiàng)目說明

參數(shù)名類型說明

filePath

String

文件的本地路徑

createTime

Number

文件的保存時的時間戳,從 1970/01/01 08:00:00 到當(dāng)前時間的秒數(shù)。

size

Number

文件大小,單位 B

示例 

在開發(fā)者工具中打開


圖片示例

代碼示例

<view class="wrap">
    <view class="card-area">
        <view class="display-area">  
            <image class="display-area-image" src="https://b.bdstatic.com/searchbox/icms/searchbox/img/file-pdf.png" rel="external nofollow"  mode="widthFix"></image>
            <view class="title">示例文件.pdf</view>
        </view>
        <button type="primary" bindtap="saveFile">保存文件</button>
        <button type="primary" bindtap="getSavedFileList">獲取已保存文件列表信息</button>
    </view>
</view>
Page({
    saveFile() {
        swan.downloadFile({
            url: 'https://example.com/file/xxx', // 僅為示例,并非真實(shí)的接口地址
            success: res => {
                swan.saveFile({
                    tempFilePath: res.tempFilePath,
                    success: res => {
                        this.toast('保存成功', 'none');
                    },
                    fail: err => {
                        this.toast('保存失敗,請稍后重試', 'none');
                    }
                });
            },
            fail: err => {
                this.toast('下載失敗,請稍后重試', 'none');
            }
        });
    },
    getSavedFileList() {
        swan.getSavedFileList({
            success: res => {
                swan.showModal({
                    title: 'success',
                    content: '目前保存文件數(shù)' + res.fileList.length
                });
                console.log('getSavedFileList success', res);
            },
            fail: err => {
                this.toast('fail', 'none');
                console.log('getSavedFileList fail', err);
            }
        });
    },
    toast(title, icon) {
        swan.showToast({title, icon});
    }
});

錯誤碼

Android

錯誤碼說明

1001

執(zhí)行失敗

2003

IO 異常



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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號