2014-11-06 72 views
1

我必须在我的phonegap项目中实现文件上传功能。用户应该能够从手机存储器或SD卡上传任何类型的文件。我试过input type =“file”,但在android 4.4中不支持。我也尝试过phonegap camera API,但它仅支持媒体文件。我发现cordova文件选择器插件(https://github.com/cdibened/filechooser)会有所帮助,但我无法使其工作。文件选择后,成功回调函数不会立即触发。请在下面找到我的代码,科尔多瓦的原生文件选择器插件为Android不起作用

<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); 
} 

当用户选择第二个文件时,第一个成功回调函数被触发。同样,当用户选择第三个文件(使用Android 4.4.2进行测试)时,第二个成功的回调函数也会被触发。我无法解决这个问题。 “filechooser.open”的第一个参数是强制性的吗?我必须通过支持所有文件类型?

在插件文档中,它写道:“您应该在您的Manifest以及LocalStorageProvider.Authorority字段中更改com.ianhanniballake.localstorage.documents”。我必须改变什么?

任何建议,非常感谢。

回答

0

phonegap/cordova不支持输入文件类型。抱歉。

您需要使用文件传输插件如本例所示,在这里How to upload file with Phonegap and JqueryMobile?讨论:

function onDeviceReady() { 

    // Retrieve image file location from specified source 
    navigator.camera.getPicture(uploadPhoto, 
           function(message) { alert('get picture failed'); }, 
           { quality: 50, 
           destinationType: navigator.camera.DestinationType.FILE_URI, 
           sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY } 
           ); 

} 

function uploadPhoto(imageURI) { 
    var options = new FileUploadOptions(); 
    options.fileKey="file"; 
    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png'; 
    options.mimeType="text/plain"; 

    var params = new Object(); 

    options.params = params; 

    var ft = new FileTransfer(); 
    ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options); 
} 

function win(r) { 
    console.log("Code = " + r.responseCode); 
    console.log("Response = " + r.response); 
    console.log("Sent = " + r.bytesSent); 
} 

function fail(error) { 
    alert("An error has occurred: Code = " + error.code); 
    console.log("upload error source " + error.source); 
    console.log("upload error target " + error.target); 
} 
+4

但是,你将如何处理的方案是在不图像/视频文件,因为科尔多瓦的摄像头插件API只允许选择图片而不是其他文件类型? – 2014-12-26 10:03:19

相关问题