HarmonyOS request.downloadFile报错

调用下载接口遇到下方的错误码及提示需要如何修改呢?“file operation error set path permission fail”。

HarmonyOS
2024-09-06 11:40:38
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

简单的下载demo,可以参考下,先获取context,再根据context 获取到应用的files路径。

import request from '@ohos.request'; 
import common from '@ohos.app.ability.common'; 
import { BusinessError } from '@ohos.base'; 
let context = getContext(this) as common.UIAbilityContext; 
let filesDir = context.filesDir; 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Button('下载').onClick(()=>{ 
          request.downloadFile(context.getApplicationContext(), { 
            url: 'https://example/141/2?index.html', 
            filePath: filesDir + '/001.mp4', 
            enableMetered:true 
          }).then((downloadTask: request.DownloadTask) => { 
            let progresCallback = (receivedSize: number, totalSize: number) => { 
              console.info("downloadddd1 receivedSize:" + receivedSize + " totalSize:" + totalSize); 
            }; 
            let pauseCallback = () => { 
              console.info('Downloadddd task pause.'); 
            }; 
            //开启进度回调 
            downloadTask.on('progress', progresCallback); 
            //开启暂停回调 
            downloadTask.on('pause', pauseCallback); 
            //开启下载完成回调 
            downloadTask.on('complete', async () => { 
              const taskInfo = await downloadTask.getTaskInfo(); 
              console.info('downloaddddTask1 complete:'+`status: ${taskInfo.status}`); 
            }) 
          }).catch((err: BusinessError) => { 
            console.error(`Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`); 
          }) 
        }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-09-06 15:14:07
相关问题
request.downloadFile的url兼容处理
348浏览 • 1回复 待解决
requestdownloadFile与Task有什么区别
1962浏览 • 1回复 待解决
request.uploadFile(context, uploadConfig) 报错
562浏览 • 1回复 待解决
: { request: WebResourceRequest}){} 报错
1807浏览 • 1回复 待解决
HarmonyOS react-native-fs不支持downloadFile
483浏览 • 1回复 待解决
HarmonyOS request 下载问题
56浏览 • 1回复 待解决
HarmonyOS request.uploadFile问题
35浏览 • 1回复 待解决