HarmonyOS 鸿蒙Next web里面里面的video标签自带下载与快进按钮
HarmonyOS 鸿蒙Next web里面里面的video标签自带下载与快进按钮
点击下载按钮的事件应该怎么处理
2 回复
可通过setDownloadDelegate()向Web组件注册一个DownloadDelegate来监听页面触发的下载任务。资源由Web组件来下载,Web组件会通过DownloadDelegate将下载的进度通知给应用。详情参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-webview-V5#webdownloaddelegate11
也可以参考下面的简易demo
import web_webview from '@ohos.web.webview'
import business_error from '@ohos.base'
@Entry
@Component
struct Page6201 {
delegate: web_webview.WebDownloadDelegate = new web_webview.WebDownloadDelegate();
@State message: string = 'Hello World';
webviewController: web_webview.WebviewController = new web_webview.WebviewController();
build() {
RelativeContainer() {
Web({ src: $rawfile("aaa.html"), controller: this.webviewController })
.onControllerAttached(()=>{
try {
this.delegate.onBeforeDownload((webDownloadItem: web_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: web_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: web_webview.WebDownloadItem) => {
console.log("download failed guid: " + webDownloadItem.getGuid());
// 下载任务失败的错误码。
console.log("download failed guid: " + webDownloadItem.getLastErrorCode());
})
this.delegate.onDownloadFinish((webDownloadItem: web_webview.WebDownloadItem) => {
// 下载任务完成后处理
console.log("download finish guid: " + webDownloadItem.getGuid());
})
this.webviewController.setDownloadDelegate(this.delegate);
} catch (error) {
let e:business_error.BusinessError = error as business_error.BusinessError;
console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
}
})
}
.height('100%')
.width('100%')
}
}