FileSystemManager.mkdirSync

2020-08-26 16:17 更新

解釋:創(chuàng)建目錄的同步接口。

Web 態(tài)說明:受瀏覽器限制,Web 態(tài)不支持文件系統(tǒng)相關(guān)功能,調(diào)用該方法會拋出一個標(biāo)準(zhǔn)的 Error 對象。

方法參數(shù):String dirPath, Boolean recursive

dirPath參數(shù)說明:創(chuàng)建的目錄路徑。

recursive參數(shù)說明:是否在遞歸創(chuàng)建該目錄的上級目錄后再創(chuàng)建該目錄。如果對應(yīng)的上級目錄已經(jīng)存在,則不創(chuàng)建該上級目錄。如 dirPath 為 a/b/c/d 且 recursive 為 true,將創(chuàng)建 a 目錄,再在 a 目錄下創(chuàng)建 b 目錄,以此類推直至創(chuàng)建 a/b/c 目錄下的 d 目錄。。

若接口調(diào)用失敗,會拋出一個標(biāo)準(zhǔn)的Error對象

示例 

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


圖片示例

代碼示例

<view class="wrap">
    <button type="primary" bindtap="mkdirSync">創(chuàng)建目錄</button>
</view>
Page({
    onLoad() {
        this.fileSystemManager = swan.getFileSystemManager();
    },
    mkdirSync() {
        try {
            let result = this.fileSystemManager.mkdirSync(
                `${swan.env.USER_DATA_PATH}/mkDir/newDir`,
                true,
            );
            swan.showToast({
                title: '創(chuàng)建成功',
                icon: 'none'
            });
            console.log('mkdirSync success', result);
        }
        catch (err) {
            swan.showToast({
                title: `${err}`,
                icon: 'none'
            });
            console.log('mkdirSync fail', err);
        }
    }
});


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號