Upload File in Nativescript Webview with iframe

回眸只為那壹抹淺笑 提交于 2019-12-02 09:03:28

问题


My problem is I have one webview Containing Dynamic Form or form which is created dynamic with WP Builders and that is included in iframe and i want to select file from application in nativescript this will not allow me select file.

So I want to try

How to load files into webview in native script

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

But problem is form is dynamic is there any script which will detect click event of file input inside frame and also put file path to it when file is selected if we can detect event for file input click.

another issue is what if there are more than one file input in dynamic form


回答1:


I Got Answer by Implementing WebChromeClient In android

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

and class MyWebChromeClient is here

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);
            });
    }
}


来源:https://stackoverflow.com/questions/51174340/upload-file-in-nativescript-webview-with-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!