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)
        })
      })
    })
  })
}
相关推荐
EatFans几秒前
Electron 打包与自动更新完全指南:electron-builder、latest.yml、app-update.yml 与自建更新服务器实战
前端
小凯在掘金几秒前
为什么要有访问器? 你不知道的对象属性
前端·javascript
OpenTiny社区几秒前
HC 2026 回顾|OpenTiny NEXT 解锁 Web 应用智能化新范式
前端·开源·github
时光少年1 分钟前
Android HWC退化与防治方法
前端
涛涛ing1 分钟前
Remix 3 RC 发布:一个不再依赖 React 的全栈框架,正在重新定义“元框架”的边界
前端
codigger1 分钟前
ObjectSense:一门千行内核、把可靠性写进骨子里的面向对象脚本语言
开发语言·编程·编程语言
闪耀之光M782 分钟前
Vite配置文件解析
前端
PC2005_cloud2 分钟前
RabbitMQ 在 Spring Boot 中的完整使用
前端·后端
掘金挖土2 分钟前
前端手摸手跑路之 AI 应用开发(八)
前端·后端
风骏时光牛马3 分钟前
产品功能可用性测试报告
前端