Javascript Nativescript应用程序不在WebView上打开文件选择器
我有一个带有WebView的应用程序Nativescript。这些WebView打开一个url,该url有一个输入类型文件。 当我触摸这个输入时,什么也没有发生。 我尝试扩展WebChromeClient类,并调用了showFileChooser方法,但什么也没发生Javascript Nativescript应用程序不在WebView上打开文件选择器,javascript,android,nativescript,nativescript-vue,Javascript,Android,Nativescript,Nativescript Vue,我有一个带有WebView的应用程序Nativescript。这些WebView打开一个url,该url有一个输入类型文件。 当我触摸这个输入时,什么也没有发生。 我尝试扩展WebChromeClient类,并调用了showFileChooser方法,但什么也没发生 让myWebChromeClientClass=android.webkit.WebChromeClient.extend({ onShowFileChooser:函数(WebView、ValueCallback、FileChoos
让myWebChromeClientClass=android.webkit.WebChromeClient.extend({
onShowFileChooser:函数(WebView、ValueCallback、FileChooserParams){
log(“onShowFileChooser”);
//我在这里要做什么?
}
});
让myWebChromeClient=新的myWebChromeClientClass();
webview.android.setWebChromeClient(myWebChromeClient);
我不知道我必须做什么。问题是WebView非常有限。但是,您可以使用插件调用文件选择器。 在我的解决方案中,我使用了 完整的代码是:
fileCallback(filePathCallback){
log(“fileCallback”);
让context=imagePicker.create({
模式:“单身”,
mediaType:imagePicker.ImagePickerMediaType.Any
});
返回此.startSelection(上下文,filePathCallback);
},
startSelection(上下文、filePathCallback){
console.log(“开始选择”);
让abc=context.authorize(),然后(()=>{
返回context.present();
})
。然后((选择)=>{
selection.forEach((已选)=>{
让path=selected.android;
让file=fs.file.fromPath(path);
this.file_path=file.path;
this.file_path=“file://”+this.file_path;
让results=Array.create(android.net.Uri,1);
结果[0]=android.net.Uri.parse(this.file\u路径);
filePathCallback.onReceiveValue(结果);
});
}).catch(函数(e){
控制台日志(e);
});
}
让TNSWebChromeClient=android.webkit.WebChromeClient.extend({
onShowFileChooser:函数(视图、值回调、fileChooserParams){
log(“onShowFileChooser”);
_这个.fileCallback(valueCallback);
返回true;
}
});
设thsWebChromeClient=new TNSWebChromeClient();
webview.android.setWebChromeClient(thsWebChromeClient);
问题是WebView非常有限。但是,您可以使用插件调用文件选择器。
在我的解决方案中,我使用了
完整的代码是:
fileCallback(filePathCallback){
log(“fileCallback”);
让context=imagePicker.create({
模式:“单身”,
mediaType:imagePicker.ImagePickerMediaType.Any
});
返回此.startSelection(上下文,filePathCallback);
},
startSelection(上下文、filePathCallback){
console.log(“开始选择”);
让abc=context.authorize(),然后(()=>{
返回context.present();
})
。然后((选择)=>{
selection.forEach((已选)=>{
让path=selected.android;
让file=fs.file.fromPath(path);
this.file_path=file.path;
this.file_path=“file://”+this.file_path;
让results=Array.create(android.net.Uri,1);
结果[0]=android.net.Uri.parse(this.file\u路径);
filePathCallback.onReceiveValue(结果);
});
}).catch(函数(e){
控制台日志(e);
});
}
让TNSWebChromeClient=android.webkit.WebChromeClient.extend({
onShowFileChooser:函数(视图、值回调、fileChooserParams){
log(“onShowFileChooser”);
_这个.fileCallback(valueCallback);
返回true;
}
});
设thsWebChromeClient=new TNSWebChromeClient();
webview.android.setWebChromeClient(thsWebChromeClient);
webViewLoaded(args:EventData):任何{
const webView:webView=args.object;
if(webView.android){
constfilechooser=1;
让message=null;
让活动=application.android.context;
const onActivityResult=(请求代码、结果代码、数据)=>{
}
//@ts忽略
让myChrome=android.webkit.WebChromeClient.extend({
onShowFileChooser:(webView、filePathCallback、fileChooserParams)=>{
openfile(webView、filePathCallback、fileChooserParams)。然后(值=>{
console.log(值)
})
返回true;
}
});
setWebChromeClient(新的myChrome());
}
}
函数openfile(webView、filePathCallback、FileChooseParams){
返回新承诺(功能(解决、拒绝){
试一试{
constfilechooser=1;
让intent=fileChooserParams.createIntent();
addCategory(“android.intent.category.OPENABLE”);
//@ts忽略
let chooser=android.content.Intent.createChooser(Intent,“文件选择器”)
application.android.foregroundActivity.startActivityForResult(选择器,文件选择器);
var活性=
application.android.foregroundActivity | | application.android.startActivity;
activity.onActivityResult=函数(请求代码、结果代码、数据){
//检查我们正在响应的请求
if(requestCode==文件选择器){
//@ts忽略
if(resultCode==android.app.Activity.RESULT\u确定){
if(data!=null&&requestCode==filechooser&&filePathCallback!=null){
//@ts忽略
让value=android.webkit.WebChromeClient.FileChooserParams.parseResult(resultCode,data);
文件路径
webViewLoaded(args: EventData): any {
const webView: WebView = <WebView>args.object;
if (webView.android) {
const filechooser = 1;
let message = null;
let activity = application.android.context;
const onActivityResult = (requestCode, resultCode, data) => {
}
// @ts-ignore
let myChrome = android.webkit.WebChromeClient.extend({
onShowFileChooser: (webView, filePathCallback, fileChooserParams) => {
openfile(webView, filePathCallback, fileChooserParams).then(value => {
console.log(value)
})
return true;
}
});
webView.android.setWebChromeClient(new myChrome());
}
}
function openfile(webView, filePathCallback, fileChooserParams) {
return new Promise(function(resolve, reject) {
try {
const filechooser = 1;
let intent = fileChooserParams.createIntent();
intent.addCategory("android.intent.category.OPENABLE");
// @ts-ignore
let chooser = android.content.Intent.createChooser(intent, "File Chooser")
application.android.foregroundActivity.startActivityForResult(chooser, filechooser);
var activity =
application.android.foregroundActivity || application.android.startActivity;
activity.onActivityResult = function(requestCode, resultCode, data) {
// Check which request we're responding to
if (requestCode === filechooser) {
// @ts-ignore
if (resultCode === android.app.Activity.RESULT_OK) {
if (data != null && requestCode == filechooser && filePathCallback != null) {
// @ts-ignore
let value = android.webkit.WebChromeClient.FileChooserParams.parseResult(resultCode, data);
filePathCallback.onReceiveValue(value);
}
return resolve({
response: 'success'
});
// @ts-ignore
} else if (resultCode === android.app.Activity.RESULT_CANCELED) {
return resolve({
response: 'cancelled'
});
} else {
return resolve({
response: 'failed'
});
}
}
};
} catch (ex) {
reject(ex.toString());
}
});
}