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 
相关推荐
爱勇宝21 小时前
一个家庭成长小程序的 MVP 复盘:看到用户在用我很欣慰
微信小程序·产品·设计
帅帅的记忆1 天前
uniapp项目调用原生android studio的sdk文件(即aar文件)
android·uni-app·android studio
宠友信息1 天前
Spring Boot与Redis ZSet实现仿小红书源码双列瀑布流推荐
java·大数据·前端·spring boot·redis·mysql·uni-app
这是个栗子2 天前
uni-app微信小程序开发:高频核心 API(三)
微信小程序·小程序·uni-app
quweiie2 天前
thinkphp8结合jwt与微信小程序接口鉴权
微信小程序·小程序·thinkphp接口·接口鉴权
didiplus4 天前
我在GitHub刷到一个诗词API,顺手写了款小程序
微信小程序
大白要努力!4 天前
uni-app 全面入门速查手册
前端·uni-app
李剑一4 天前
uni-app x 实现本地 JSON 文件的导入导出(APP端可用)
android·前端·uni-app
2501_915106324 天前
iOS 软件测试工具性能监控、日志分析 KeyMob、Instruments等
android·ios·小程序·https·uni-app·iphone·webview
小码哥0684 天前
医院陪诊小程序怎么开发-医院陪诊小程序源码功能-微信小程序 医院健康陪诊陪护系统
微信小程序·小程序·医院陪诊·陪诊系统·陪诊陪诊