javascript - 使用 iframe 在 Nativescript Webview 中上传文件

标签 javascript webview nativescript

我的问题是我有一个包含动态表单的 webview 或使用 WP Builders 动态创建并包含在 iframe 中的表单,我想从 nativescript 中的应用程序中选择文件,这不允许我选择文件。

所以我想试试

How to load files into webview in native script

http://shripalsoni.com/blog/nativescript-webview-native-bi-directional-communication/

但问题是表单是动态的,如果我们可以检测文件输入点击事件,是否有任何脚本可以检测框架内文件输入的点击事件,并在选择文件时将文件路径放入其中。

另一个问题是,如果以动态形式输入多个文件怎么办

最佳答案

我通过在 android 中实现 WebChromeClient 得到了答案

 let webview: WebView = this.webViewRef.nativeElement;
 let myWebChromeClient: MyWebChromeClient = new MyWebChromeClient();
 webview.android.setWebChromeClient(myWebChromeClient);

MyWebChromeClient 类在这里

import * as imagepicker from "nativescript-imagepicker";
import * as fs from "file-system";

export class MyWebChromeClient extends android.webkit.WebChromeClient {
    file_path:string;
    constructor() {
        super();

        return global.__native(this);
    }

    onShowFileChooser(webView,filePathCallback,fileChooserParams) {

         this.filepathCallback(filePathCallback);

        return true;
    }

    filepathCallback(filePathCallback)
    {
        let context = imagepicker.create({
            mode: "single",
            mediaType: imagepicker.ImagePickerMediaType.Any
        });
       this.startSelection(context,filePathCallback);
    }

    startSelection(context,filePathCallback) {
        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);
                    return this.file_path;
                });
            }).catch(function (e) {
                console.log(e);
            });
    }
}

关于javascript - 使用 iframe 在 Nativescript Webview 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51174340/

相关文章:

javascript - 单个浏览器选项卡中的多个 Javascript 解释器

objective-c - 在 Cocoa 中保存 Webarchive 而不使用图像

java - 如何在关闭阶段后清除 Javafx Webview 内存使用情况

android - 如何在 native 后台服务中使用( Angular )HTTP 客户端 - NativeScript

javascript - 如何在 nativescript-vue 弹窗中使用 vue 组件?

javascript - SoundCloud 流 URL 404

javascript - 为什么填充 anchor 标记会创建重复的超链接?

javascript - 从键盘输入后启用/禁用文本区域

javascript - 输入 iframe 在第二次点击 ionic v1 中停止在 IOS 上工作

带抽屉和登录页面的 Nativescript 4.1 app-root