HarmonyOS 浏览本地pdf文件 -

按照https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-pdf-preview-V5的说法,使用下面的路径file:///data/storage/el2/base/haps/entry/cache/E2024000000277137977.pdf,Web提示未能加载PDF文档。

HarmonyOS
2024-12-27 15:32:19
874浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

参考下面的代码:

import { webview } from '@kit.ArkWeb';
import common from '@ohos.app.ability.common';
import request from '@ohos.request';
import { BusinessError } from '@ohos.base';
import fs from '@ohos.file.fs';
@Entry
@Component
export struct FCPdfWebViewPage {
  @State flag: Boolean = false;
  @State webController: WebviewController = new webview.WebviewController();
  @State path: string = '';
  build() {
    Column() {
      Button('下载', { type: ButtonType.Normal })
        .onClick(() => {
          console.log('下载')
          let context = getContext(this) as common.UIAbilityContext;
          let fileDir = context.filesDir;
          try {
            request.downloadFile(context, {
              url: 'xxxxx',
              filePath: fileDir + '/test.pdf'
            }).then((downloadTask: request.DownloadTask) => {
              downloadTask.on('complete', () => {
                let resFile = fs.openSync(fileDir + '/test.pdf', fs.OpenMode.READ_WRITE);
                const dateStr = (new Date().getTime()).toString()
                let newPath = fileDir + dateStr + resFile.name;
                fs.copyFileSync(resFile.fd, newPath);
                let realUri = 'file://' + newPath;
                this.path = realUri
                this.flag = true
                console.log(this.path)
              })
              downloadTask.on('progress', (receivedSize: number, totalSize: number) => {
                console.log(receivedSize.toString())
              })
              downloadTask.on('fail', (err: number) => {
                console.log(err.toString())
              })
            })
          } catch (error) {
            let err: BusinessError = error as BusinessError;
            console.error(`Invoke downloadFile failed, code is ${err.code}, message is ${err.message}`)
          }
        })
        .height('50vp')
        .width('100%')
        .backgroundColor(Color.Blue)
      if (this.flag) {
        Web({ src: this.path, controller: this.webController })
          .domStorageAccess(true)
          .javaScriptAccess(true)
          .zoomAccess(true)
          .aspectRatio(1)
          .overviewModeAccess(true)
          .imageAccess(true)
          .onlineImageAccess(true)
          .fileAccess(true)
          .databaseAccess(true)
          .multiWindowAccess(true)
          .geolocationAccess(true)
          .horizontalScrollBarAccess(true)
          .verticalScrollBarAccess(true)
          .cacheMode(CacheMode.Default)
          .mixedMode(MixedMode.All)
      } else {
        Web({ src: 'xxxxx', controller: this.webController })
          .domStorageAccess(true)
          .javaScriptAccess(true)
          .zoomAccess(true)
          .aspectRatio(1)
          .overviewModeAccess(true)
          .imageAccess(true)
          .onlineImageAccess(true)
          .fileAccess(true)
          .databaseAccess(true)
          .multiWindowAccess(true)
          .geolocationAccess(true)
          .horizontalScrollBarAccess(true)
          .verticalScrollBarAccess(true)
          .cacheMode(CacheMode.Default)
          .mixedMode(MixedMode.All)
      }
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
分享
微博
QQ
微信
回复
2024-12-27 17:34:39


相关问题
提供浏览PDF文件的框架
679浏览 • 1回复 待解决
HarmonyOS 使用PDF kit预览PDF文件
867浏览 • 1回复 待解决
HarmonyOS PDF文件预览
846浏览 • 1回复 待解决
HarmonyOS PDF文件保存
958浏览 • 1回复 待解决
HarmonyOS 怎么打开PDF文件
1018浏览 • 1回复 待解决
怎么下载网络上PDF保存到本地?
4728浏览 • 1回复 待解决
HarmonyOS 常见Office文件浏览支持
712浏览 • 1回复 待解决
HarmonyOS 文件管理查找pdf文件并上传
910浏览 • 1回复 待解决
HarmonyOS 是否支持pdf、Word文件预览?
1279浏览 • 1回复 待解决
点击文字跳转pdf文件
784浏览 • 1回复 待解决