FileSystemManager.appendFileSync

2020-08-26 16:16 更新

解释:在文件结尾追加内容的同步接口。

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

方法参数:String filePath, String/ArrayBuffer data, String encoding

filePath参数说明:文件路径。

data参数说明:要追加的文本。

encoding参数说明:指定写入文件的字符编码。

encoding 的合法值

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

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <button type="primary" bindtap="appendFileSync">在文件结尾追加内容</button>
</view>
Page({
    data: {
        filePath: ''
    },
    onLoad() {
        this.fileSystemManager = swan.getFileSystemManager();
        swan.downloadFile({
            url: 'https://b.bdstatic.com/docs/demo.txt',
            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.txt`);
            }
        });
    },
    appendFileSync() {
        try {
            let result = this.fileSystemManager.appendFileSync(
                `${swan.env.USER_DATA_PATH}/a.txt`,
                'appendFileData',
            );
            swan.showToast({
                title: 'appendFileSync success',
                icon: 'none'
            });
            console.log('appendFileSync success');
        }
        catch (err) {
            swan.showToast({
                title: `${err}`,
                icon: 'none'
            });
            console.log('appendFileSync fail', err);
        }
    }
});


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

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号