HarmonyOS 文件权限问题

在使用axios下载网络图片到本地cachedir到时候报

file operation error set path permission fail, download
path: /data/storage/el2/base/cache/BBComicImageCache/ad/a700e47e05e2759d2d4b60c4c5f71c4d.tmp
  • 1.
  • 2.

(通过context.cacheDIr()获取)

HarmonyOS
2024-12-23 16:43:10
3489浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

可以先参考下这个demo;

import axios, { AxiosError, AxiosProgressEvent, AxiosResponse } from '@ohos/axios'
import fs from '@ohos.file.fs';

@Entry
@Component
struct Index {
  downTest() {
    let filePath = getContext(this).cacheDir + '/next-pc.jpg'
    // 下载。如果文件已存在,则先删除文件。
    try {
      fs.accessSync(filePath);
      fs.unlinkSync(filePath);
    } catch(err) {}

    axios({
      url: 'https://xxx.png',
      method: 'get',
      // context: getContext(this),
      filePath: filePath ,
    }).then((res:AxiosResponse)=>{
      console.info("result: " + JSON.stringify(res.data));
    }).catch((error:AxiosError)=>{
      console.error("error:" + JSON.stringify(error));
    })
  }
  build() {
    RelativeContainer() {
      Button('下载').onClick(() => {
        this.downTest()
      })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

备用参考,axios更多用法git链接:

https://gitee.com/openharmony-sig/ohos_axios#%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6%E7%A4%BA%E4%BE%8B

分享
微博
QQ
微信
回复
2024-12-23 19:32:28


相关问题
HarmonyOS文件读写权限问题
1256浏览 • 1回复 待解决
HarmonyOS 权限声明文件配置问题
1630浏览 • 1回复 待解决
单框架native侧文件读取权限问题
2749浏览 • 1回复 待解决
HarmonyOS定位权限问题
1356浏览 • 1回复 待解决
HarmonyOS 权限弹窗问题
906浏览 • 1回复 待解决
HarmonyOS 权限配置问题
1422浏览 • 1回复 待解决
HarmonyOS 权限弹窗问题
888浏览 • 1回复 待解决
HarmonyOS picker权限问题
1054浏览 • 1回复 待解决
HarmonyOS相册权限问题
1435浏览 • 1回复 待解决
HarmonyOS 相机权限问题
1460浏览 • 1回复 待解决
HarmonyOS native层文件读写权限
858浏览 • 1回复 待解决
HarmonyOS 应用权限问题咨询
892浏览 • 1回复 待解决
HarmonyOS权限申请问题
1241浏览 • 1回复 待解决
HarmonyOS 相机权限问题咨询
749浏览 • 1回复 待解决
HarmonyOS 屏幕共享权限问题
785浏览 • 1回复 待解决
HarmonyOS 权限申请拒绝问题
1006浏览 • 1回复 待解决
HarmonyOS 重复进行文件权限申请
469浏览 • 1回复 待解决
HarmonyOS权限导出沙盒文件
728浏览 • 1回复 待解决
HarmonyOS 文件的读写权限如何获得?
1610浏览 • 1回复 待解决
HarmonyOS 申请定位权限失败问题
1244浏览 • 1回复 待解决