HarmonyOS web组件如何使用html加载下载到设备本地的文件

使用web组件加载test.html,期望通过拦截html中的http网络请求,改为加载本地文件,现在的情况是可以加载rawfile文件,如何加载下载到设备本地的文件,如:/data/storage/el2/base/haps/entry/files/car.pag

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

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-webview-V5#setpathallowinguniversalaccess12

示例参考:

import web_webview from '@ohos.web.webview';
import fs from '@ohos.file.fs';
import fileio from '@ohos.fileio';
import common from '@ohos.app.ability.common';
import axios from '@ohos/axios'
import buffer from '@ohos.buffer';

@Entry
@Component
struct GetFile {

  @State message: string = 'Hello World';
  // 获取应用文件路径
  context = getContext(this) as common.UIAbilityContext;
  webviewController: web_webview.WebviewController = new web_webview.WebviewController();
  responseweb: WebResourceResponse = new WebResourceResponse()
  aboutToAppear() {
    // 配置Web开启调试模式
    web_webview.WebviewController.setWebDebuggingAccess(true);
  }

  hasFile(filePath:string){
    let b =  fs.accessSync(filePath)
    return b
  }

  async getFile(url : string) {

    // const regex = new RegExp('/[^/]+/g', 'gim');
    // console.log(url+" 123456")

    // const fileName = url.match(regex);
    const fileName = url.split("/").pop();
    console.log(fileName+" 123456")
    const filePath = this.context.cacheDir + '/'+fileName;
    console.log(filePath+' 123456')

    const flag=this.hasFile(filePath);
    console.log(flag+"123456")
    if(!flag){
      await this.downloadImage(url, filePath);
    }

    // const fd : number = await fileio.open(filePath);
    let file = await fs.openSync(filePath, fs.OpenMode.READ_ONLY);
    console.log(file+"1234567")
    let arrayBuffer = new ArrayBuffer(4096);
    console.log(file.fd+" 123456")
    return file.fd;
  }

  async downloadImage(url : string, filePath:string) {
    try {
      await axios({
        method: "get",
        url,
        context: this.context,
        filePath: filePath });
    } catch (error) {
      console.error('[Demo] axios error');
    }
  }

  build() {

    Column() {
      Web({ src: $rawfile('catch.html'), controller: this.webviewController })
        .onInterceptRequest((event) => {
          if (event) {
            console.log('url123456:' + event.request.getRequestUrl())
          }

          const url = event!.request.getRequestUrl();

          // const regExp = new RegExp("^(http|https)[w/-]+(.jpg)$");
          console.log(url.endsWith(".jpg") + '123456')
          if (!url.endsWith(".jpg")) return null;
          try {
            // const responseweb = new WebResourceResponse();
            let url2='https://xxx.jpg';
            this.getFile(url2).then(fd => {
              this.responseweb.setResponseData(fd);
              this.responseweb.setResponseCode(200);
              this.responseweb.setReasonMessage('OK');
              this.responseweb.setResponseIsReady(true);
              console.log(fd+" 1234567")
            });
            this.responseweb.setResponseMimeType('image/*');
            this.responseweb.setResponseIsReady(false);
            console.log(this.responseweb.getResponseData().toString+"123456")
            return this.responseweb;

          } catch (error) {
            console.error(`[Demo]Code: ${error.code},Message: ${error.message} `);
            return null
          }

        })
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
web组件html文件加载
802浏览 • 1回复 待解决
HarmonyOS Web组件加载html文件异常
538浏览 • 1回复 待解决
HarmonyOS web组件如何加载本地字库?
247浏览 • 1回复 待解决
HarmonyOS Web组件如何加载html字符串
655浏览 • 1回复 待解决
HarmonyOS 如何加载本地沙盒中html
87浏览 • 1回复 待解决
HarmonyOS webview加载本地html问题
509浏览 • 1回复 待解决
HarmonyOS web组件怎么加载html字符串
27浏览 • 1回复 待解决
Web如何使用loadData加载复杂Html字符串
2369浏览 • 1回复 待解决