javascript - Nativescript 应用程序无法在 WebView 上打开文件选择器

标签 javascript android nativescript nativescript-vue

我有一个带有 WebView 的应用程序 Nativescript。这些WebView打开一个url,这个url有一个input类型的文件。 当我触摸这个输入时,什么也没有发生。 我尝试扩展 WebChromeClient 类并调用 onShowFileChooser 方法,但没有任何反应。

let myWebChromeClientClass = android.webkit.WebChromeClient.extend({
          onShowFileChooser: function (WebView, ValueCallback, FileChooserParams) {
            console.log("onShowFileChooser");
           // What i have to do here?
          }
        });
        let myWebChromeClient = new myWebChromeClientClass();
 webview.android.setWebChromeClient(myWebChromeClient);

我不知道我必须做什么。

最佳答案

问题是WebView的功能非常有限。但是,您可以使用插件来调用文件选择器。 在我的解决方案中,我使用了 imagepicker plugin 完整代码为:

fileCallback(filePathCallback) {
        console.log("fileCallback");
        let context = imagePicker.create({
          mode: "single",
          mediaType: imagePicker.ImagePickerMediaType.Any
        });
        return this.startSelection(context, filePathCallback);
      },
      startSelection(context, filePathCallback) {
        console.log("startSelection");
        let abc = context.authorize().then(() => {
          return context.present();
        })
          .then((selection) => {
            selection.forEach((selected) => {
              let path = selected.android;
              let file = fs.File.fromPath(path);
              this.file_path = file.path;
              this.file_path = "file://" + this.file_path;
              let results = Array.create(android.net.Uri, 1);
              results[0] = android.net.Uri.parse(this.file_path);
              filePathCallback.onReceiveValue(results);
            });
          }).catch(function (e) {
            console.log(e);
          });
      }

     let TNSWebChromeClient = android.webkit.WebChromeClient.extend({
          onShowFileChooser: function (view, valueCallback, fileChooserParams) {
            console.log("onShowFileChooser");
            _this.fileCallback(valueCallback);
            return true;
          }
        });
    let thsWebChromeClient = new TNSWebChromeClient();
     webview.android.setWebChromeClient(thsWebChromeClient);

关于javascript - Nativescript 应用程序无法在 WebView 上打开文件选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59295644/

相关文章:

android - 在带有角度的 native 脚本中,当键盘打开时,它将我的元素(操作栏)推出屏幕(ANDROID)

javascript - Angularjs http post 到 c# 不起作用

javascript - 如何捕获错误 net::ERR_ABORTED

android - SwipeRefreshLayout 刷新与 fragmentTransactions 冲突

android - 单击按钮打开应用程序(如果已安装)否则在 Android 中打开 Play 商店?

ios - 需要 Google 移动广告 SDK 常量的值

ios - 如何设置 RadDataForm ListPicker 的选择器的样式

javascript - Google 脚本运行时优化

javascript - 带星号的正则表达式电话号码

android - 为什么响应式电子邮件在石蕊试纸上效果很好,但在发送后就坏了