HarmonyOS 鸿蒙Next request.downloadFile 出现错误文件路径
HarmonyOS 鸿蒙Next request.downloadFile 出现错误文件路径
try {
request.downloadFile(globalThis.mycontext,
{ url: wangzhi,filePath:’${globalThis.wenjianlujing}/123.jpg’})
.then((data) => {
}).catch((err) => {
console.error('Failed to request the download. Cause: ’ + JSON.stringify(err));
})
} catch (err) {
console.error('err.code : ’ + err.code + ', err.message : ’ + err.message);
}
}
export default class EntryAbility extends UIAbility {
onCreate(want, launchParam) {
globalThis.wenjianlujing = this.context.filesDir
hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onCreate’);
}
真机测试,始终显示:err.code:13400002
err.message:bad file path Doenload File Path Valid
更多关于HarmonyOS 鸿蒙Next request.downloadFile 出现错误文件路径的实战教程也可以访问 https://www.itying.com/category-93-b0.html
import request from '[@ohos](/user/ohos).request';
import common from '[@ohos](/user/ohos).app.ability.common';
export function down(fileUrl: string){
let context = getContext(this) as common.UIAbilityContext;
const fileSuffixName = fileUrl.split('.')[fileUrl.split('.').length - 1]
const fileFullName = fileUrl.split('/')[fileUrl.split('/').length - 1]
console.log('context.filesDir', context.filesDir)
const filePath = `${context.filesDir}/${new Date().getTime() + '.' + fileSuffixName}`;
console.error(filePath);
try {
request.downloadFile(context, { url: fileUrl,filePath: filePath }, (err, data) => {
if (err) {
console.error('Failed to request the download. Cause: ' + JSON.stringify(err));
return;
}
// 监听文件下载的实时情况 receivedSize已下载 totalSize总大小
data.on('progress', (receivedSize, totalSize) => {
console.info("upload totalSize:" + totalSize + " receivedSize:" + receivedSize);
})
// 监听文件下载完成
data.on('complete', () => {
console.info('Download task completed.');
})
});
} catch (err) {
console.error('err.code : ' + err.code + ', err.message : ' + err.message);
}
}
更多关于HarmonyOS 鸿蒙Next request.downloadFile 出现错误文件路径的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
老哥,你这代码运行后文件会下载在虚拟机的哪里呀
我今天遇到这个报错了,我的解决办法是创建文件parent目录 createFileParent(path: string) { let end = path.lastIndexOf(’/’) if (end == -1 || end == path.length - 1) return let parentPath = path.substring(0, end + 1) if (fs.accessSync(parentPath)) return fs.mkdirSync(parentPath) }
仅有url参数,也报错bad file path Doenload File Path Valid
request.downloadFile(globalThis.mycontext,{url: wangzhi})
真机型号P60 HarmonyOS 3.1.0
DevEco Studio 3.1 Release 构建版本:3.1.0.500
没有
filePath:'${globalThis.wenjianlujing}/123.jpg'
request.downloadFile(globalThis.mycontext,{url: wangzhi})
也报错
针对HarmonyOS 鸿蒙Next中request.downloadFile
出现错误文件路径的问题,以下是一些专业的解答:
首先,确保提供的filePath
参数是一个完整的文件路径,并且所有父级路径都已创建。如果路径不存在,需要先使用fs.mkdir
或类似方法创建路径。
其次,检查路径中是否包含非法字符或格式错误,例如路径分隔符不正确、文件名过长等。确保路径符合HarmonyOS的文件系统规范。
此外,还需要确认应用是否具有写入该路径的权限。在HarmonyOS中,应用默认只能访问自己的沙盒目录,如果需要访问其他目录,需要在manifest文件中声明相应的权限。
如果以上步骤都正确无误,但问题依旧存在,可能是系统或API的bug。此时,可以尝试更新HarmonyOS系统到最新版本,或者查看华为开发者官方网站上的相关文档和公告,了解是否有已知的问题和解决方案。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。