Question

I've been implementing upload picture functionality in Phonegap. My server side code is already ready.

Ive installed plugins File and FileTransfer but when i try to upload a picture to my server, the code always fail and will always go to fail() function. I cannot determine whats the error since error.code, error.source, error.target is always null.

Why am i getting null on error.code? I cannot determine whats wrong here so i really need your help.

This is my code :

           var pictureSource = navigator.camera.PictureSourceType;
           var destinationType = navigator.camera.DestinationType;

                    function clearCache() {
                        navigator.camera.cleanup();
                    }

                    var retries = 0;
                    function onCapturePhoto(fileURI) {
                        var win = function (r) {
                            clearCache();
                            retries = 0;
                            alert('Done!');
                            console.log(r);
                        }

                        var fail = function (error) {
                            alert("An error has occurred: Code = " + error.code);
                            console.log("upload error source " + error.source);
                            console.log("upload error target " + error.target);
                            if (retries == 0) {
                                retries ++
                                setTimeout(function() {
                                    onCapturePhoto(fileURI)
                                }, 1000)
                            } else {
                                retries = 0;
                                clearCache();
                                alert('Ups. Something wrong happened!');
                            }
                        }

                        var options = new FileUploadOptions();
                        options.fileKey = "profilepic";
                        options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
                        options.mimeType = "image/jpeg";
                        options.chunkedMode = false;
                        options.params = {atoken : app.atoken, user_id : window.localStorage.getItem("user_id")}; // if we need to send parameters to the server request
                        var ft = new FileTransfer();
                        //alert(app.base_url + "/apiuser/updatePic");
                        ft.upload(fileURI, encodeURI(app.base_url + "/apiuser/updatePic"), win, fail, options);
                    }

                    function capturePhoto() {
                        navigator.camera.getPicture(onCapturePhoto, onFail, {
                            quality: 100,
                            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
                            destinationType: destinationType.FILE_URI
                        });
                    }

                    function onFail(message) {
                        alert('Failed because: ' + message);
                    }

                    capturePhoto();
Was it helpful?

Solution

Fixed it in config.xml

I used

<gap:plugin name="org.apache.cordova.file"/>
<gap:plugin name="org.apache.cordova.file-transfer"/>

instead of

<feature name="File">
   <param name="android-package" value="org.apache.cordova.FileUtils" />
</feature>
<feature name="FileTransfer">
   <param name="android-package" value="org.apache.cordova.FileTransfer" />
</feature>

Im using phonegap 3.4.0 for Android

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top