ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-14 03:38:50  阅读:259  来源: 互联网

标签:nativescript javascript webview


我的问题是我有一个包含动态表单的Web视图,或者使用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);
            });
    }
}

标签:nativescript,javascript,webview
来源: https://codeday.me/bug/20191014/1912334.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有