web组件onShowFileSelector事件,里面使用弹框(相机/文件按钮),或者判断前端input要调用的类型

web组件onShowFileSelector事件,能区分前端调用intput的类型accept吗?或者我在抽取的webview组件中怎么使用弹框提示用户选择相机或者文件。

HarmonyOS
2024-11-07 09:58:15
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

可以参考如下demo来实现:

// xxx.ets 
import web_webview from '@ohos.web.webview'; 
import picker from '@ohos.file.picker'; 
import { BusinessError } from '@ohos.base'; 
import { promptAction } from '@kit.ArkUI'; 
 
@Entry 
@Component 
struct Index { 
  controller: web_webview.WebviewController = new web_webview.WebviewController() 
  dialogController: CustomDialogController = new CustomDialogController({ 
    builder: ShowDialog({}) 
  }) 
  @Provide event: CustomEvent | undefined = undefined 
  build() { 
    Column() { 
      Web({ src: $rawfile('index.html'), controller: this.controller }) 
        .onShowFileSelector((event) => { 
          this.dialogController.open() 
          this.event = event 
          return true 
        }) 
    } 
  } 
} 
 
@CustomDialog 
struct ShowDialog { 
  @Consume event: CustomEvent 
  @State choice: string = '' 
  controller: CustomDialogController = new CustomDialogController({ 
    builder: ShowDialog({}) 
  }) 
 
  handle(param: CustomEvent) { 
    if (this.choice === '文件') { 
      console.log(`当前的选择是2${this.choice}`) 
      console.log('MyFileUploader onShowFileSelector invoked') 
      const documentSelectOptions = new picker.DocumentSelectOptions(); 
      let uri: string | null = null; 
      const documentViewPicker = new picker.DocumentViewPicker(); 
      documentViewPicker.select(documentSelectOptions).then((documentSelectResult) => { 
        uri = documentSelectResult[0]; 
        console.info('documentViewPicker.select to file succeed and uri is:' + uri); 
        if (this.event) { 
          this.event.result.handleFileList([uri]); 
        } 
      }).catch((err: BusinessError) => { 
        console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 
      }) 
    } else if (this.choice === '相机') { 
      promptAction.showToast({ message: '正在开发中' }) 
    } 
  } 
 
  build() { 
    Column({ space: 10 }) { 
      Button('文件') 
        .onClick(() => { 
          this.choice = '文件' 
          this.handle(this.event) 
          this.controller.close() 
        }) 
      Button('相机') 
        .onClick(() => { 
          this.choice = '相机' 
          this.handle(this.event) 
          this.controller.close() 
        }) 
    }.width("100%") 
    .height(400) 
    .justifyContent(FlexAlign.Center) 
  } 
} 
 
interface CustomEvent { 
  result: FileSelectorResult, 
  fileSelector: FileSelectorParam 
}
分享
微博
QQ
微信
回复
2024-11-07 17:24:32
相关问题
HarmonyOS API调用与Dialog
260浏览 • 0回复 待解决
HarmonyOS ArkTS如何调用web前端js库
104浏览 • 1回复 待解决
HarmonyOS 自定义组件问题
478浏览 • 1回复 待解决
HarmonyOS 嵌套实现
43浏览 • 1回复 待解决
HarmonyOS CustomDialog报错
43浏览 • 1回复 待解决
HarmonyOS 支付输入
42浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人