HarmonyOS 鸿蒙Next Web组件的下载能力报错 [web_download_manager.cpp:129] [DOWNLOAD] had RegisterDownloadCallback
HarmonyOS 鸿蒙Next Web组件的下载能力报错 [web_download_manager.cpp:129] [DOWNLOAD] had RegisterDownloadCallback 按照官方文档的示例,实现Web组件的下载能力,但是点击setDownloadDelegate按钮报错,也没有实现下载功能!
在应用的rawfile中创建index.html以及download.html。应用启动后会创建一个Web组件并加载index.html,点击setDownloadDelegate按钮向Web组件注册一个DownloadDelegate,点击页面里的下载按钮的时候会触发一个下载任务,在DownloadDelegate中可以监听到下载的进度。
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate();
build() {
Column() {
Button('setDownloadDelegate')
.onClick(() => {
try {
this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => {
console.log("will start a download.");
// 传入一个下载路径,并开始下载。
// 如果传入一个不存在的路径,则会下载到默认/data/storage/el2/base/cache/web/目录。
webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName());
})
this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => {
// 下载任务的唯一标识。
console.log("download update guid: " + webDownloadItem.getGuid());
// 下载的进度。
console.log("download update guid: " + webDownloadItem.getPercentComplete());
// 当前的下载速度。
console.log("download update speed: " + webDownloadItem.getCurrentSpeed())
})
this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => {
console.log("download failed guid: " + webDownloadItem.getGuid());
// 下载任务失败的错误码。
console.log("download failed guid: " + webDownloadItem.getLastErrorCode());
})
this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => {
console.log("download finish guid: " + webDownloadItem.getGuid());
})
this.controller.setDownloadDelegate(this.delegate);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: $rawfile('index.html'), controller: this.controller })
}
}
}
加载的html文件
<!-- index.html -->
<!DOCTYPE html>
<html>
<body>
<a href='data:text/html,<h1>Hello, World!</h1>' download='download.html'>下载</a>
</body>
</html>
待下载的html文件。
<!-- download.html -->
<!DOCTYPE html>
<html>
<body>
<h1>download test</h1>
</body>
</html>
报错信息:
;
delegate:web_webview.WebDownloadDelegate = new web_webview.WebDownloadDelegate();
@State flag:string = '';
@State progress:string = '0';
@State speed:string = '';
@State isSuccess:string = '';
build() {
Column(){
Text('下载的任务标识:'+this.flag)
Text('下载的进度:'+this.progress+'%')
Text('下载的速度:'+this.speed)
Text('下载的情况:'+this.isSuccess)
Button('setDownloadDelegate')
.onClick(()=>{
try{
this.controller.startDownload('https://codeload.github.com/AppGalleryConnect/agc-template-market-harmonyos-demos/zip/refs/heads/main');
this.delegate.onBeforeDownload((webDownloadItem:web_webview.WebDownloadItem)=>{
console.log('准备开始下载');
webDownloadItem.start('/docs/storage/Users/currentUser/Documents/'+webDownloadItem.getSuggestedFileName());
})
this.delegate.onDownloadUpdated((webDownloadItem:web_webview.WebDownloadItem)=>{
this.flag = webDownloadItem.getGuid();
this.progress = JSON.stringify(webDownloadItem.getPercentComplete());
this.speed = JSON.stringify(webDownloadItem.getCurrentSpeed());
})
this.delegate.onDownloadFailed((webDownloadItem:web_webview.WebDownloadItem)=>{
this.isSuccess = '下载失败';
})
this.delegate.onDownloadFinish((webDownloadItem:web_webview.WebDownloadItem)=>{
this.isSuccess = '下载成功';
})
this.controller.setDownloadDelegate(this.delegate);
}catch(error){
let e:business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
}
})
Web({ src: 'www.baidu.com', controller: this.controller })
}
}
}
2、使用Web组件发起一个下载任务
import web_webview from '@ohos.web.webview';
@Entry
@Component
struct Index {
aboutToAppear() {
web_webview.WebviewController.setWebDebuggingAccess(true);
}
//配置Web开启调试模式
webController: web_webview.WebviewController = new web_webview.WebviewController();
build() {
Column() {
// Web component loading H5.
Web({ src: $rawfile('pdf/index.html'), controller: this.webController })
}
}
}
index.html页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./css/pdfh5.css" />
<link rel="stylesheet" href="./css/style.css" />
<script src="./js/jquery-3.6.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="./js/pdf.js" type="text/javascript" charset="utf-8"></script>
<script src="./js/pdf.worker.js" type="text/javascript" charset="utf-8"></script>
<script src="./js/pdfh5.js" type="text/javascript" charset="utf-8"></script>
<style>
html, body {
height: 100%;
}
</style>
</head>
<body>
<div id="view"></div>
<script>
new Pdfh5("#view", {
pdfurl: 'https://www.gov.cn/zhengce/pdfFile/2023_PDF.pdf',
});
</script>
</body>
</html>
在HarmonyOS鸿蒙Next中,Web组件的下载能力报错 [web_download_manager.cpp:129] [DOWNLOAD] had RegisterDownloadCallback
通常与下载回调注册相关。该错误表明在注册下载回调时发生了问题,可能是由于回调函数未正确实现或注册流程出现异常。
具体原因可能包括:
- 回调函数未正确定义或实现。
- 下载管理器未正确初始化。
- 注册回调时传入的参数不符合要求。
解决该问题需要检查代码中与下载回调相关的部分,确保回调函数正确实现,并且注册流程符合鸿蒙Next的API规范。
在HarmonyOS鸿蒙Next中,Web组件的下载能力报错 [web_download_manager.cpp:129] [DOWNLOAD] had RegisterDownloadCallback
通常与下载回调注册相关。可能的原因包括:
- 重复注册回调:确保
RegisterDownloadCallback
只被调用一次,避免多次注册。 - 回调函数未实现:检查是否正确定义了下载回调函数。
- 权限问题:确认应用已获取必要的网络和存储权限。
- Web组件配置错误:检查
WebView
或Web
组件的配置是否正确。
建议检查代码逻辑,确保回调注册和权限配置无误。