我必须在我的phonegap 项目中实现文件上传功能。用户应该能够从手机内存或 SD 卡上传任何类型的文件。我使用 jQuery Mobile 框架设计的应用程序屏幕。我尝试了 input type="file",但 android 4.4 不支持它。我也尝试过phonegap相机API,但它仅支持媒体文件。我发现了一些科尔多瓦插件(exm1,exm2)。但这些插件使用自定义 UI。我想调用本机文件浏览器来选择文件,它必须在 Android 和 iPhone 平台上工作。有没有办法实现同样的效果?
我找到了 cordova 文件选择器插件(https://github.com/cdibened/filechooser)对android平台会有帮助,但我无法让它工作。选择文件后,成功回调函数不会立即触发(使用 android 4.4.2 测试)。请在下面找到我的代码,
<input type="file" id="fileinput" name="fileinput"/>
$("#fileinput").bind('click',function(){
console.log("choose file selected");
filechooser.open( {}, fileChooseSuccess, fileChooseFailed );
});
function fileChooseSuccess(data) {
var filepath = data.filepath;
console.log("file path:"+filepath);
}
function fileChooseFailed(msg) {
console.log(msg);
}
我能够让你的插件 FileChooser 正常工作。
但有些事情是必须要做的。
您需要使用编辑器打开以下内容
这是我使用的演示代码:
<html>
<head>
<title>Hello World</title>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", function(){
var success = function(data) {
alert("File chosen: " + data.filepath);
};
var error = function(msg) {
console.log( msg );
};
$('#fileinput').click(function(e) {
filechooser.open({}, success,error);
});
});
</script>
</head>
<body>
<input type="file" id="fileinput" name="fileinput"/>
</body>
</html>
另外,请注意,作者打算在 KitKat 4.4.4 中使用它。它可能适用于较低版本,但他不确定。
请注意,HTML5 选择窗口与此窗口之间的唯一区别是“内部存储”选项。
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)