Nativescript 应用程序不在 WebView 上打开文件选择器
Nativescript application dont open file chooser on WebView
我有一个带有 WebView 的应用程序 Nativescript。这些WebView打开一个url,而这个url有一个输入类型的文件。
当我触摸这个输入时,没有任何反应。
我正在尝试扩展 WebChromeClient class 并调用 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);
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());
}
});
}
希望对大家有所帮助...))
我有一个带有 WebView 的应用程序 Nativescript。这些WebView打开一个url,而这个url有一个输入类型的文件。 当我触摸这个输入时,没有任何反应。 我正在尝试扩展 WebChromeClient class 并调用 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);
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());
}
});
}
希望对大家有所帮助...))