📌 从基础库 3.125.4 版本开始支持
写文件
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | string | 是 | 目标文件路径(本地路径) | |
data | string/ArrayBuffer | 是 | 要写入的内容(文本或二进制) | |
encoding | string | utf8 | 否 | 字符编码(仅支持特定类型) |
success | function | 否 | 写入成功的回调 | |
fail | function | 否 | 写入失败的回调 | |
complete | function | 否 | 调用结束的回调 |
⚠️ 小红书平台仅支持以下编码:
其他编码如ascii/hex/ucs2等不支持
const fs = xhs.getFileSystemManager(); // 异步写入文本 fs.writeFile({ filePath: `${xhs.env.USER_DATA_PATH}/log.txt`, data: "Hello 小红书", encoding: "utf8", success() { console.log("写入成功"); }, fail(res) { console.error("写入失败:", res.errMsg); } }); // 同步接口 try { const res = fs.writeFileSync( `${xhs.env.USER_DATA_PATH}/hello.txt`, 'some text or arrayBuffer', 'utf8' ) console.log(res) } catch(e) { console.error(e) }