HarmonyOS App内置浏览器打开pdf文件,点击下载按钮,没有反应

App内置浏览器WebView打开一个pdf文件,右上角有一个下载按钮,但点击没有任何响应,Webview的onLoadIntercept也没有任何回调给出。

需求:

方案一: 内置webview实现这个下载功能。

方案二:给出屏蔽这个下载按钮的方法。

方案三:onLoadIntercept给出下载按钮点击事件,我们自行下载此pdf。

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

参考demo onBeforeDownload 处理

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'

@Component
export struct downloadPdf {
  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}`)
        }
      })
      TextArea({text: 'https://xxx.pdf' })
      Web({ src: 'https://xxx.pdf', controller: this.controller })
    }.margin({ top: 200 })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
浏览器下载文件如何导入鸿蒙
7307浏览 • 1回复 待解决
HarmonyOS web下载文件点击反应
111浏览 • 1回复 待解决
HarmonyOS 如何在浏览器唤起app
611浏览 • 1回复 待解决
HarmonyOS浏览器scheme链接唤起APP问题
1075浏览 • 1回复 待解决
HarmonyOS 浏览器不能通过scheme拉起app
685浏览 • 1回复 待解决
HarmonyOS跳转系统浏览器打开网页报错
713浏览 • 1回复 待解决
HarmonyOS @ohos.net.http请求没有反应
41浏览 • 1回复 待解决
安卓App或者浏览器如何跳转鸿蒙App
10017浏览 • 1回复 待解决
HarmonyOS 浏览器访问H5,并唤起App
25浏览 • 1回复 待解决
HarmonyOS 启动默认浏览器
594浏览 • 1回复 待解决