javascript - 使用 iframe 在 Nativescript Webview 中上传文件
问题描述
我的问题是我有一个包含动态表单或表单的 webview,它是使用 WP Builders 动态创建的,并且包含在 iframe 中,我想从 nativescript 中的应用程序中选择文件,这将不允许我选择文件。
所以我想试试
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);
});
}
}
推荐阅读
- azure - 如何在数据工厂中接收 http 帖子?
- flutter - Flutter how to take a photo with custom width and height
- azure - 模板语言函数 'div' 期望它的第一个参数是整数或十进制数
- javascript - 单击按钮时重置表格内容不起作用
- python - 如何提高python中虚线图的效率(速度)
- sql-server - PostgreSQL 中 SQL Server 中“@@PROCID”系统变量的等效项
- reactjs - 如何使用react redux防止对象中的重复数据
- python - Pandas Dataframe 中使用 NLTK 包的句子标记化
- javascript - 当我更改另一个 SelectBox 的值时,如何启用其他 SelectBox 值?
- python - Flask-SqlAlchemy 加载大量数据后插入速度极慢