uniapp如何修改下载文件位置

1、根据下载的方法,请见:下载的方法下载的方法

2、修改下载文件的位置

复制代码
const fileWriter = () => {
  // 请求本地系统文件对象 plus.io.PRIVATE_WWW:应用运行资源目录常量
  plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fobject) => {
    // fs.root是根目录操作对象DirectoryEntry
    const fileName = formatTime(new Date()) + '.txt'; // 文件名
    fobject.root.getFile(fileName, {
      create: true
    }, (fileEntry) => {
      fileEntry.file((file) => {
        console.log('file :>> ', file)
        // create a FileWriter to write to the file
        fileEntry.createWriter((writer) => {
          console.log('writer :>> ', writer)
          // 返回页面的数据
          writer.seek(file.size)
          // 写入可以加入需要写入的数据
          let content = "{points:" + JSON.stringify(polyline.value[0].points) + ",pointArr:" + JSON.stringify(pointArr.value)+ "}"
          writer.write(content)
          const fileReader = new plus.io.FileReader()
          fileReader.readAsText(file, 'utf-8')
          // 获取对象 
          plus.io.resolveLocalFileSystemURL(file.fullPath, (res) => {
            // 获取公共下载目录
            plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, (downloadFs) => {
              console.log('downloadFs :>> ', downloadFs)
              // 移动文件到下载目录(若需复制则用 copyTo)
              fileEntry.copyTo(downloadFs.root, fileName, (newFileEntry) => {
                console.log('文件已移动到下载目录:', newFileEntry.fullPath);
                
                // 7. 打开文件
                uni.openDocument({
                  filePath: newFileEntry.fullPath,
                  success: () => console.log('文件打开成功'),
                  fail: (err) => console.error('打开文件失败:', err)
                });
              }, (err) => {
                console.error('移动文件失败:', err);
              });
            });
            
          })
        }, (e) => {
          console.log('e :>> ', e)
        })
      })
    })
  })
}
相关推荐
我不是疯子是傻子42 分钟前
Qt CAN通信周期发送抖动?实测定时器精度校准与时间戳补偿方案
开发语言·数据库·qt
IT_陈寒1 小时前
Vue的双向绑定把我坑惨了,原来这个场景不能用
前端·人工智能·后端
hunterandroid1 小时前
[Android 从零到一] Compose LazyColumn 性能优化:key、稳定性与重组治理
android·前端
IT爱学堂1 小时前
尚硅谷 - 2025年3月Java+AI大模型应用开发
java·开发语言·人工智能
有点。1 小时前
C++认识数
开发语言·c++
lichenyang4532 小时前
从一次团队邀请开始:用 React、NestJS 与 Socket.IO 做可靠的实时通知
前端
vtian2 小时前
一篇文章吃透 Monorepo:pnpm + Turborepo + Changesets 全流程实战(含 8 个踩坑)
前端
默_笙2 小时前
❗ 点击计数按钮,为什么"峨眉队"也跟着重新渲染?React.memo 说:我记住了
前端·javascript
31535669132 小时前
DeepSeek Harness 发布后,我没急着跑 Demo,先把 `.agents/` 翻了一遍
前端·后端·github
名字还没想好☜2 小时前
Next.js 用 Server Components 直连数据库:去掉 API 层的边界,和三条别踩的安全红线
前端·javascript·数据库·安全·react·next.js