首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用iframe上传Nativescript Webview中的文件

用iframe上传Nativescript Webview中的文件
EN

Stack Overflow用户
提问于 2018-07-04 13:00:42
回答 1查看 1.1K关注 0票数 1

我的问题是,我有一个包含动态表单或表单的网页视图,它是用webview创建的,它包含在iframe中,我想从本地的应用程序中选择文件,这将不允许我选择文件。

所以我想试试

How to load files into webview in native script

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

但问题是形式是动态的,如果我们能够检测到文件输入点击事件,那么任何脚本都会检测到帧内文件输入的单击事件,并在选择文件时将文件路径放置到该脚本中。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 10:31:57

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

代码语言:javascript
运行
复制
 let webview: WebView = this.webViewRef.nativeElement;
 let myWebChromeClient: MyWebChromeClient = new MyWebChromeClient();
 webview.android.setWebChromeClient(myWebChromeClient);

MyWebChromeClient类在这里

代码语言:javascript
运行
复制
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);
            });
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51174340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档