FileSystemManager.renameSync

2020-08-26 16:17 更新

解释:重命名文件的同步接口,可以把文件从 oldPath 移动到 newPath。

Web 态说明:受浏览器限制,Web 态不支持文件系统相关功能,调用该方法会抛出一个标准的 Error 对象。

方法参数:String oldPath, String newPath

oldPath参数说明:源文件/源目录路径。

newPath参数说明:目标路径。

若接口调用失败,会抛出一个标准的Error对象

示例 

在开发者工具中打开



图片示例

代码示例

<view class="wrap">
    <button type="primary"  bindtap="saveFile">保存服务器文件到本地</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: `文件预下载完成,临时路径为${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);
        }
    }
});



    以上内容是否对您有帮助:
    在线笔记
    App下载
    App下载

    扫描二维码

    下载编程狮App

    公众号
    微信公众号

    编程狮公众号