HarmonyOS 鸿蒙Next 如何通过一个文档的下载链接 将文档下载至应用目录内
HarmonyOS 鸿蒙Next 如何通过一个文档的下载链接 将文档下载至应用目录内
暂时没有根据下载链接查询下载任务的Api,有需求的话,可以使用key-value进行保存
文件的上传下载可参考文档: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/js-apis-request-V13
报401是401 参数检查失败,详细参考:
参考下以下demo:
import { BusinessError, request } from '[@kit](/user/kit).BasicServicesKit';
import fs from '[@ohos](/user/ohos).file.fs';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
[@State](/user/State) message: string = 'Hello World';
api9_task: request.DownloadTask | undefined;
downLoad = async () => {
let downUrl =
"https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%9B%BE%E7%89%87&step_word=&hs=0&pn=32&spn=0&di=7375936315981824001&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=3324321790%2C325371109&os=3708118008%2C3566968155&simid=4282501645%2C823704451&adpicid=0&lpn=0&ln=1737&fr=&fmq=1721286234192_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined©right=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fimage109.360doc.com%2FDownloadImg%2F2021%2F09%2F1409%2F230310966_1_20210914091333446%26refer%3Dhttp%3A%2F%2Fimage109.360doc.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1723878237%26t%3D46a16ced985317cee6ab5df08a743e4c&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bnma15v_z%26e3Bv54AzdH3Fv5gpjgpAzdH3Fd8AzdH3Fal89AzdH3FalAzdH3Fdncclaa_llc98dmd9_z%26e3Bfip4s&gsm=1e&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined&dyTabStr=MCwzLDEsMiw2LDQsNSw4LDcsOQ%3D%3D&lid=7873798213617569258"
try {
let filePath = getContext().cacheDir + "/test6666";
let isHas = await fs.access(filePath);
if (isHas) {
await fs.unlink(filePath);
}
request.downloadFile(getContext(), {
url: downUrl, header: {
'User-Agent': 'Chrome/124.0.0.0',
}, filePath: filePath
}).then((data: request.DownloadTask) => {
let downloadTask: request.DownloadTask = data;
let progressCallback = (receivedSize: number, totalSize: number) => {
console.info("download receivedSize:" + receivedSize + " totalSize:" + totalSize);
};
downloadTask.on('progress', progressCallback);
downloadTask.getTaskInfo().then((downloadInfo: request.DownloadInfo) => {
console.info('Succeeded in querying the download task')
}).catch((err: BusinessError) => {
console.error(`Failed to query the download task. Code: ${err.code}, message: ${err.message}`)
});
let complateCallback = () => {
console.info("download success-----.");
};
downloadTask.on('complete', complateCallback);
let failCallback = (e: number) => {
console.info(`download fail----- ${e}.`);
};
downloadTask.on('fail', failCallback);
}).catch((err: BusinessError) => {
console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`);
})
} catch (err) {
console.error(`Failed to request the download. err: ${JSON.stringify(err)}`);
}
}
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(this.downLoad)
}
.height('100%')
.width('100%')
}
}
在HarmonyOS(鸿蒙)系统中,通过文档的下载链接将其下载至应用目录内,通常需要利用系统提供的文件访问与下载API。以下是基本步骤概述:
-
权限配置:确保你的应用在
manifest.json
中声明了必要的文件读写权限,特别是外部存储访问权限,以便能够下载并保存文件至应用目录。 -
使用下载管理器:HarmonyOS提供了下载管理器API,可用于处理文件的下载。你可以创建一个下载任务,指定下载链接、保存路径(应用目录内的路径)以及其他相关参数。
-
处理下载回调:下载任务创建后,需监听下载进度和完成状态。通过回调方法,你可以在下载完成后获取文件路径,并进行后续处理。
-
保存文件至应用目录:下载完成后,文件通常已保存在指定的应用私有目录内。你可以通过文件访问API进一步操作该文件,如读取内容、展示给用户等。
请注意,实际操作中需考虑异常处理,如网络中断、存储不足等情况。此外,根据HarmonyOS版本的不同,API的具体调用方式可能有所差异。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html