uniapp 构建本地txt跨平台小程序、h5、app通用

一般本地构建txt需要上传oss时,需要拿到临时文件地址filePath之类才能进行后续的上传。 但注意不同端使时方式不同

1 wx 是小程序环境的全局对象

arduino 复制代码
// 小程序
#ifdef MP-WEIXIN
// export const createTxt = data => {
//   return new Promise((resolve, reject) => {
//     const fs = wx.getFileSystemManager()
//     const filePath = `${wx.env.USER_DATA_PATH}/${Date.now()}.txt`
//     fs.writeFile({
//       filePath,
//       data,
//       encoding: 'utf8',
//       success(res) {
//         resolve(filePath)
//       },
//       fail(res) {
//         reject(res)
//       }
//     })
//   })
// }
#endif 

2 网页环境FileURL.createObjectURL 是 Web API);

arduino 复制代码
// 网页输出时
#ifdef H5
// export const createTxt = text => {
//   let file = new File([text], `${Date.now()}`, { type: 'text/plain' })
//   return URL.createObjectURL(file)
// }
#endif

3 app 环境(Android/iOS)用的是 plus 对象,不支持 wx 和 Web API

javascript 复制代码
// app平台专用
#ifdef APP-PLUS
export const createTxt = (data) => {
  return new Promise((resolve, reject) => {
    // 获取应用沙盒目录
    const fileName = `${Date.now()}.txt`;
    plus.io.requestFileSystem(
      plus.io.PRIVATE_DOC, // 沙盒路径,安全可写
      (fs) => {
        fs.root.getFile(
          fileName,
          { create: true },
          (entry) => {
            entry.createWriter((writer) => {
              writer.onwrite = () => resolve(entry.fullPath);
              writer.onerror = reject;
              writer.write(data);
            }, reject);
          },
          reject
        );
      },
      reject
    );
  });
};
#endif 
相关推荐
夏天测14 小时前
微信小程序自动化漏洞挖掘流水线:从缓存提取到密钥验证全流程实战
python·网络安全·微信小程序·漏洞挖掘
it-102419 小时前
微信小程序短视频去水印/抖音短视频去水印/免费去水印源码
微信小程序·小程序·视频去水印
xshirleyl19 小时前
uniapp小兔鲜儿day3
uni-app
Geek_Vison2 天前
2026 跨端框架横评:FinClip、Taro、uni-app、Remax、mPaaS 五款工具技术+业务双维度测评
小程序·uni-app·taro·mpaas·小程序容器
kidding7232 天前
高效备忘清单工具类小程序
前端·计算机网络·微信小程序·小程序
RuoyiOffice2 天前
从 0 到 1 搭建 RuoyiOffice:30 分钟跑通后端+前端+移动端
前端·spring boot·uni-app·开源·oa·ruoyioffice·hrm
Geek_Vison2 天前
APP集成了50多个小程序后,如何搭建一个小程序管理平台来管理这些小程序~
小程序·uni-app·apache·mpaas·小程序容器
前端 贾公子2 天前
小程序蓝牙打印探索与实践 (最终章)
前端·微信小程序·小程序
小羊Yveesss2 天前
2026年个人能做微信小程序吗?
微信小程序·小程序
kidding7232 天前
BMI 健康测量仪工具类小程序
前端·微信小程序·小程序