2014-11-06 31 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

相關問題