HarmonyOS 加载html,图片显示不出来

解决Web组件本地资源跨域问题,使用这样的去加载 html,图片显示不出来,直接通过 raw 加载正常显示。这是怎么回事?

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  webviewController: webview.WebviewController = new webview.WebviewController();
  // 构造域名和本地文件的映射表
  schemeMap = new Map([
    ["https://www.index.com/c02/s01f01.html", "c02/s01f01.html"],
    ["https://www.index.com/c02/s01f02.html", "c02/s01f02.html"],
  ])
  // 构造本地文件和构造返回的格式mimeType
  mimeTypeMap = new Map([
    ["c01/s01f01.html", 'text/html'],
    ["c02/s01f01.html", 'text/html'],
    ["c02/s01f02.html", 'text/html'],
  ])

  build() {
    Row() {
      Column() {
        // 针对本地index.html,使用http或者https协议代替file协议或者resource协议,并且构造一个属于自己的域名。
        // 本例中构造www.index.com为例。
        Web({ src: "https://www.index.com/c02/s01f01.html", controller: this.webviewController })
          .javaScriptAccess(true)
          .fileAccess(true)
          .domStorageAccess(true)
          .geolocationAccess(true)
          .width("100%")
          .height("100%")
          .onInterceptRequest((event) => {
            if (!event) {
              return;
            }
            // 此处匹配自己想要加载的本地离线资源,进行资源拦截替换,绕过跨域
            if (this.schemeMap.has(event.request.getRequestUrl())) {
              let rawfileName: string = this.schemeMap.get(event.request.getRequestUrl())!;
              let mimeType = this.mimeTypeMap.get(rawfileName);
              if (typeof mimeType === 'string') {
                let response = new WebResourceResponse();
                // 构造响应数据,如果本地文件在rawfile下,可以通过如下方式设置
                response.setResponseData($rawfile(rawfileName));
                response.setResponseEncoding('utf-8');
                response.setResponseMimeType(mimeType);
                response.setResponseCode(200);
                response.setReasonMessage('OK');
                response.setResponseIsReady(true);
                return response;
              }
            }
            return null;
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 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.
HarmonyOS
2024-12-25 14:58:39
573浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

看一下mimetype是不是image图片类型的

let mimeType = this.mimeTypeMap.get(rawfileName);

分享
微博
QQ
微信
回复
2024-12-25 17:24:56


相关问题
HarmonyOS scrollbar显示不出来
766浏览 • 1回复 待解决
HarmonyOS 地图加载不出来,只显示背景
1827浏览 • 1回复 待解决
HarmonyOS 地图加载不出来
1048浏览 • 1回复 待解决
HarmonyOS Lottie动画加载不出来
876浏览 • 1回复 待解决
开启混淆后,卡片加载不出来
7208浏览 • 1回复 待解决
HarmonyOS list最后一个显示不出来
945浏览 • 1回复 待解决
HarmonyOS 多模块下悬浮窗显示不出来
1193浏览 • 1回复 待解决
HiLog日志打印不出来?
12216浏览 • 3回复 已解决