W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
解釋:重命名文件的同步接口,可以把文件從 oldPath 移動到 newPath。
Web 態(tài)說明:受瀏覽器限制,Web 態(tài)不支持文件系統(tǒng)相關(guān)功能,調(diào)用該方法會拋出一個標準的 Error 對象。
方法參數(shù):String oldPath, String newPath
oldPath
參數(shù)說明:源文件/源目錄路徑。
newPath
參數(shù)說明:目標路徑。
若接口調(diào)用失敗,會拋出一個標準的Error
對象
<view class="wrap">
<button type="primary" bindtap="saveFile">保存服務(wù)器文件到本地</button>
<button type="primary" bindtap="renameSync">重命名文件</button>
</view>
Page({
data: {
filePath: ''
},
onLoad() {
this.fileSystemManager = swan.getFileSystemManager();
},
saveFile() {
swan.downloadFile({
url: 'https://smartprogram.baidu.com/docs/img/logo.png',
success: res => {
swan.showToast({
title: `文件預(yù)下載完成,臨時路徑為${res.tempFilePath}`,
icon: 'none'
});
// 此為 “本地臨時文件” 路徑。
this.data.filePath = res.tempFilePath;
// 將 “本地臨時文件” 持久化成 “本地用戶文件”。
this.fileSystemManager.saveFileSync(res.tempFilePath, `${swan.env.USER_DATA_PATH}/a.text`);
}
});
},
renameSync() {
try {
let result = this.fileSystemManager.renameSync(
`${swan.env.USER_DATA_PATH}/a.text`,
`${swan.env.USER_DATA_PATH}/b.text`
);
swan.showToast({
title: 'renameSync success',
icon: 'none'
});
console.log('renameSync success', result);
}
catch (err) {
swan.showToast({
title: JSON.stringify(err),
icon: 'none'
});
console.log('renameSync fail', err);
}
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: