HarmonyOS web组件的PDF的下载保存按钮没有用

使用web组件的PDF文档预览能力,页面顶部会有工具栏,其中之一是保存,怎么实现保存的功能,现在点击保存下载按钮没有作用

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

需要使用setDownloadDelegate.ets,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-download-0000001820999905-V5

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://xxx.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 })
  }
}

目前只能先将pdf下载到沙箱后然后通过文管的接口下载copy到文件管理器。整个流程就是先点击setDownloadDelegate按钮向Web注册一个监听类,然后点击startDownload主动发起了一个下载, 该下载任务也会通过设置的DownloadDelegate来通知app下载的进度。

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-download-V5

分享
微博
QQ
微信
回复
2天前
相关问题
关于WebView提示没有用户手势问题
10557浏览 • 1回复 待解决
怎么下载网络上PDF保存到本地?
3981浏览 • 1回复 待解决
HarmonyOS Web组件打开pdf页面
152浏览 • 1回复 待解决
没有加载PDF组件
355浏览 • 1回复 待解决
HarmonyOS web组件加载pdf问题
929浏览 • 1回复 待解决
HarmonyOS Web组件加载pdf预览
493浏览 • 1回复 待解决
基于WebDownloadDelegatePDF下载预览
693浏览 • 1回复 待解决
HarmonyOS PDF文件保存
305浏览 • 1回复 待解决