0

I want to transfer a image file from a photolibrary (iOS & Android) to php with Phonegap 3.7.0 . I get a code error null with Android and code error 3 on iOS, I have tried multiple solutions but that didn't work for me. The Phonegap Javascript below comes from the official Phonegap documentation so I don't know what I'm doing wrong.

I hope someone can help me,

Thanks in advance!

HTML:

<input ng-click="onDeviceReady()" type="image" class="addphoto" ng-src="img/addPhotoButton.png" id="photo1" alt="addPhotoButton"/>

Javascript:

       $scope.onDeviceReady = function() {
        // 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);
        options.mimeType="image/jpeg";

        var params = {};
        params.value1 = "test";
        params.value2 = "param";
        options.chunkedMode = false;
        options.headers = { Connection: "close"};
        options.params = params;

        var ft = new FileTransfer();
        ft.upload(imageURI, "photo.php", win, fail, options);
        console.log(imageURI);
        console.log(params);
        console.log(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);
}

PHP:

function postPhoto(){
        print_r($_FILES);
        $new_image_name = "photo1.jpg";
        move_uploaded_file($_FILES["file"]["tmp_name"], "/temp".$new_image_name);
    }

Phonegap xml config file:

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

                <access origin="*" />

            <!-- Features -->
            <feature name="Accelerometer">
                <param name="ios-package"     value="CDVAccelerometer" />
            </feature>
            <feature name="Geolocation">
                <param name="ios-package"     value="CDVLocation" />
            </feature>
            <feature name="Geolocation">
                <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
            </feature>
            <feature name="Camera">
                <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
            </feature>
            <feature name="Camera">
                <param name="ios-package" value="CDVCamera" />
            </feature>
            <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>
            <feature name="File">
                <param name="ios-package" value="CDVFile" />
            </feature>
            <feature name="FileTransfer">
                <param name="ios-package" value="CDVFileTransfer" />
            </feature>



            <feature name="http://api.phonegap.com/1.0/camera" />
            <feature name="http://api.phonegap.com/1.0/file" />
            <feature name="http://api.phonegap.com/1.0/geolocation"/>
            <feature name="http://api.phonegap.com/1.0/network" />
Tim
  • 98
  • 3
  • 14
  • 1
    on ft.upload(imageURI, "photo.php", win, fail, options); you have to use an absolute url to a server with the php, not just photo.php – jcesarmobile Apr 15 '15 at 13:46
  • 1
    Sorry for the late reaction, but your comment worked! Thanks jcesarmobile I was struggling for 3 days with this one. – Tim May 01 '15 at 14:59

0 Answers0