尝试使用turbolinks-android软件包上传文件时没有任何反应。据推测,这是因为您需要编写自己的文件上传程序处理程序?
如何做到这一点?我希望用户能够按下上传按钮然后拍照并将其附加到HTML文件字段。
另外,如何在turbolinks-ios上完成这项工作?
答案 0 :(得分:2)
您可以将WebChromeClient附加到Turbolinks webview。请注意,给定的示例适用于API21 +,当在webview中单击文件上载按钮时,较低的api使用不同的功能。
将其添加到文件顶部:
private ValueCallback<Uri[]> mUploadMessage;
private final static int REQUESTCODE_FILEPICKER = 100; // or any other int
并将其添加到onCreate()函数中:
TurbolinksSession.getDefault(this).getWebView().setWebChromeClient(new WebChromeClient() {
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
}
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("application/pdf");
startActivityForResult(Intent.createChooser(i, "Pick a file"), REQUESTCODE_FILEPICKER);
return true;
}
});
最后:将其添加到onActivityResult()函数中以处理上传的文件:
if (requestCode == REQUESTCODE_FILEPICKER) {
if (data == null ) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
return;
}
String dataString = data.getDataString();
if (dataString != null) {
Uri[] results = new Uri[]{Uri.parse(dataString)};
mUploadMessage.onReceiveValue(results);
mUploadMessage = null;
return;
}
mUploadMessage.onReceiveValue(null);
return;
}
希望这会帮助你!