FileSystemManager.saveFile

2020-08-26 16:17 更新

解釋?zhuān)罕4媾R時(shí)文件到本地。此接口會(huì)移動(dòng)臨時(shí)文件,因此調(diào)用成功后, tempFilePath 將不可用。

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

方法參數(shù):Object object

object參數(shù)說(shuō)明:

success 返回參數(shù)說(shuō)明:

示例 

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


圖片示例

代碼示例

<view class="wrap">
    <button type="primary"  bindtap="saveFile">保存臨時(shí)文件到本地</button>
</view>
Page({
    data: {
        filePath: ''
    },
    onLoad() {
        this.fileSystemManager = swan.getFileSystemManager();
        swan.downloadFile({
            url: 'https://smartprogram.baidu.com/docs/img/logo.png',
            success: res => {
                swan.showToast({
                    title: `文件預(yù)下載完成,臨時(shí)路徑為${res.tempFilePath}`,
                    icon: 'none'
                });
                // 此為 “本地臨時(shí)文件” 路徑。
                console.log(res.tempFilePath);
                this.data.filePath = res.tempFilePath;
            }
        });
    },
    saveFile() {
        this.fileSystemManager.saveFile({
            tempFilePath: this.data.filePath, // 僅為示例,實(shí)際上請(qǐng)傳真實(shí)臨時(shí)路徑地址,如 swan.downloadFile 的 tempFilePath 返回參數(shù)
            filePath: `${swan.env.USER_DATA_PATH}/a.png`,
            success: res => {
                swan.showToast({
                    title: res.errMsg,
                    icon: 'none'
                });
                console.log('saveFile success', res);
            },
            fail: err => {
                swan.showToast({
                    title: JSON.stringify(err),
                    icon: 'none'
                });
                console.log('saveFile fail', err);
            }
        });
    }
});


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)