• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// [Start web_file_upload]
17import { webview } from '@kit.ArkWeb';
18import { BusinessError } from '@kit.BasicServicesKit';
19import { picker } from '@kit.CoreFileKit';
20
21@Entry
22@Component
23struct WebComponent {
24  controller: webview.WebviewController = new webview.WebviewController();
25
26  build() {
27    Column() {
28      Web({ src: $rawfile('local.html'), controller: this.controller })
29        .onShowFileSelector((event) => {
30          console.log('MyFileUploader onShowFileSelector invoked');
31          const documentSelectOptions = new picker.DocumentSelectOptions();
32          let uri: string | null = null;
33          const documentViewPicker = new picker.DocumentViewPicker();
34          documentViewPicker.select(documentSelectOptions).then((documentSelectResult) => {
35            uri = documentSelectResult[0];
36            console.info('documentViewPicker.select to file succeed and uri is:' + uri);
37            if (event) {
38              event.result.handleFileList([uri]);
39            }
40          }).catch((err: BusinessError) => {
41            console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
42          })
43          return true;
44        })
45    }
46  }
47}
48// [End web_file_upload]
49