2

I have to implement file upload feature in my phonegap project. User should be able to upload any type of file from the phone memory or sd card. I tried input type="file", but is not supported in android 4.4. I tried phonegap camera API too, but it is supported only media files. I found cordova file chooser plugin (https://github.com/cdibened/filechooser) would be helpful, but I am unable to make it work. The success callback function is not immediately getting triggered after the file selection. Please find my code below,

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

The first success callback function is getting triggered when the user select the second file. Likewise second success callback function is getting triggered when the user select the third file (tested with Android 4.4.2).I am unable to resolve this issue. Is the first argument for “filechooser.open” is mandatory ? What I have to pass for supporting all file types?

In the plugin documentation, its written that “You should change com.ianhanniballake.localstorage.documents in your Manifest, as well as the LocalStorageProvider.AUTHORITY field”. What I have to change?

Any suggestions are greatly appreciated.

Sinu Varghese
  • 800
  • 1
  • 14
  • 39

1 Answers1

-1

input type of file is not supported in phonegap/cordova. Sorry.

You need to use the FileTransfer plugin as shown in this example and discussed here 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);
}
Community
  • 1
  • 1
wbt11a
  • 798
  • 4
  • 12
  • 4
    But how would you handle scenarios for files which are not images/ videos because cordova's camera plugin api only allows to select pictures and not other file types? – Aditya Singh Dec 26 '14 at 10:03