我有一个带有 WebView 的应用程序 Nativescript。这些WebView打开一个url,这个url有一个input类型的文件。 当我触摸这个输入时,什么也没有发生。 我尝试扩展 WebChromeClient 类并调用 onShowFileChooser 方法,但没有任何反应。
let myWebChromeClientClass = android.webkit.WebChromeClient.extend({
onShowFileChooser: function (WebView, ValueCallback, FileChooserParams) {
console.log("onShowFileChooser");
// What i have to do here?
}
});
let myWebChromeClient = new myWebChromeClientClass();
webview.android.setWebChromeClient(myWebChromeClient);
我不知道我必须做什么。
最佳答案
问题是WebView的功能非常有限。但是,您可以使用插件来调用文件选择器。 在我的解决方案中,我使用了 imagepicker plugin 完整代码为:
fileCallback(filePathCallback) {
console.log("fileCallback");
let context = imagePicker.create({
mode: "single",
mediaType: imagePicker.ImagePickerMediaType.Any
});
return this.startSelection(context, filePathCallback);
},
startSelection(context, filePathCallback) {
console.log("startSelection");
let abc = 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);
});
}).catch(function (e) {
console.log(e);
});
}
let TNSWebChromeClient = android.webkit.WebChromeClient.extend({
onShowFileChooser: function (view, valueCallback, fileChooserParams) {
console.log("onShowFileChooser");
_this.fileCallback(valueCallback);
return true;
}
});
let thsWebChromeClient = new TNSWebChromeClient();
webview.android.setWebChromeClient(thsWebChromeClient);
关于javascript - Nativescript 应用程序无法在 WebView 上打开文件选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59295644/