小程序开放平台

文档中心
基础
路由
跳转
转发
界面
网络
支付
数据缓存
媒体
位置
开放接口
设备
XHSML
性能
第三方平台
文件
removeSavedFile
openDocument
getFileSystemManager
FileSystemManager方法
access
accessSync
appendFile
appendFileSync
copyFile
copyFileSync
getSavedFileList
getFileInfo
mkdir
mkdirSync
readdir
readdirSync
rmdir
rmdirSync
rename
renameSync
readFile
readFileSync
removeSavedFile
stat
statSync
saveFile
saveFileSync
truncate
truncateSync
unlink
unlinkSync
unzip
writeFile
writeFileSync
Stats
FileStats
错误码
数据分析
画布

fs.saveFileSync

开发
>
JS API
>
文件
>
FileSystemManager方法
>
saveFileSync
>
更新时间:2025-07-23 11:46:57

📌 从基础库 3.125.4 版本开始支持

功能描述

FileSystemManager.saveFile
的同步版本

参数

名称
类型
必填
说明
tempFilePathstring临时文件路径(本地路径)
filePathstring目标存储路径(不传则自动生成)

返回值

string savedFilePath
存储后的文件绝对路径

代码示例

function saveFileSync(tempFilePath, filePath = '') {
  try {
    const fs = xhs.getFileSystemManager()
    // 调用 saveFileSync 保存文件
    const savedFilePath = fs.saveFileSync(tempFilePath, filePath);
    console.log('文件保存成功:', savedFilePath); // 存储后的文件路径
    return savedFilePath; // 返回存储后的文件路径
  } catch (err) {
    console.error('文件保存失败:', err);
    throw err; // 抛出错误以便调用方处理
  }
}

// 调用示例
try {
  const savedPath = saveFileSync('/path/to/temp/file.txt', '/path/to/save/file.txt');
  console.log('保存的文件路径:', savedPath);
} catch (err) {
  console.error('保存文件时出错:', err);
}
该文档是否对您有帮助?