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)
        })
      })
    })
  })
}
相关推荐
Ws_2 小时前
C#学习 Day2
开发语言·学习·c#
杰克尼3 小时前
天机学堂复习总结(day03-day04)
java·开发语言·redis·elasticsearch·spring cloud
tedcloud1233 小时前
RTK部署教程:构建稳定的AI Workflow环境
服务器·javascript·人工智能·typescript·ocr
x***r1513 小时前
jdk-11.0.16.1_windows使用步骤详解(附JDK 11环境变量配置与验证教程)
java·开发语言·windows
ZC跨境爬虫4 小时前
跟着 MDN 学CSS day_16:(深入掌握背景与边框的艺术)
前端·css·ui·html·tensorflow
luck_bor4 小时前
File类&递归作业
java·开发语言
道里6 小时前
花了 5 万刀用 AI 写代码之后,这是我的全部经验
前端·人工智能
Royzst7 小时前
xml知识点
java·服务器·前端
IT_陈寒7 小时前
React useEffect闭包陷阱差点把我整失业了
前端·人工智能·后端
努力努力再努力wz7 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github