HarmonyOS web组件在点击下载的时候无法进行下载

HarmonyOS
9天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

可以参考下面demo

import web_webview from '@ohos.web.webview'
import business_error, { BusinessError } from '@ohos.base'
import { picker } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController();
  delegate: web_webview.WebDownloadDelegate = new web_webview.WebDownloadDelegate();

  build() {
    Column() {
      Button('setDownloadDelegate')
        .onClick(() => {
          try {
            this.delegate.onBeforeDownload((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("======will start a download." + webDownloadItem.getSuggestedFileName());
              webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName());
              // webDownloadItem.start("/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName());
            })
            this.delegate.onDownloadUpdated((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("======download update guid: " + webDownloadItem.getGuid());
            })
            this.delegate.onDownloadFailed((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("download failed guid: " + webDownloadItem.getGuid());
            })
            this.delegate.onDownloadFinish((webDownloadItem: web_webview.WebDownloadItem) => {
              console.log("======download finish guid: " + webDownloadItem.getGuid());
              console.log("======downloadFullPath:" + webDownloadItem.getFullPath())


              let documentSaveOptions = new picker.DocumentSaveOptions();
              documentSaveOptions.newFileNames = [webDownloadItem.getSuggestedFileName()];
              let documentPicker = new picker.DocumentViewPicker();
              documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
                let uri = documentSaveResult[0];
                let path = "/data/storage/el2/base/cache/web/" + webDownloadItem.getSuggestedFileName()
                let sanFile = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
                let pubFile = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
                fs.copyFileSync(sanFile.fd, pubFile.fd)
                console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' +
                JSON.stringify(documentSaveResult));
              }).catch((err: BusinessError) => {
                console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
              });

            })
            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}`);
          }
        })
      Button('startDownload')
        .onClick(() => {
          try {
            this.controller.startDownload('https://prdc-ams.oss-cn-shenzhen.aliyuncs.com/1700182405099.pdf');
          } catch (error) {
            let e: business_error.BusinessError = error as business_error.BusinessError;
            console.error(`ErrorCode: ${e.code}, Message: ${e.message}`);
          }
        })
      Web({ src: 'www.huawei.com', controller: this.controller })
    }.margin({ top: 200 })
  }
}
分享
微博
QQ
微信
回复
9天前
相关问题
HarmonyOS web下载文件点击无反应
267浏览 • 1回复 待解决
使用Web组件下载能力Demo
566浏览 • 1回复 待解决
怎样实现Web组件下载能力?
717浏览 • 1回复 待解决
HarmonyOS flutter组件无法自动下载
209浏览 • 1回复 待解决
module资源无法下载
1014浏览 • 1回复 待解决
Web组件如何发起一个下载任务?
620浏览 • 1回复 待解决
MacOS DevEco Studio 无法下载 HarmonyOS SDK
11119浏览 • 3回复 待解决
对指定url图片进行下载保存
1229浏览 • 1回复 待解决
HarmonyOS c++ api文档无法下载
169浏览 • 1回复 待解决
huawei webview 组件下载地址
9804浏览 • 2回复 待解决