0

<script type="text/javascript">  
function uploadFromGallery() {

    // 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);
}
</script>
</head>
<body>
   <a data-role="button" onClick="uploadFromGallery();">Upload from Gallery</a> 
</body>

I want to upload capture image to a server (firebase).

I tried the full example of upload capture image (from phonegap doc http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileUploadResult ). Also I added the plugin "cordova-plugin-file , cordova-plugin-file-transfer and cordova-plugin-camera) but it didn't work.

PS: the camera capture worked. The problem is with the upload to the server.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>File Transfer Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Wait for device API libraries to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // device APIs are available
        //
        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);
            options.mimeType="image/jpeg";

            var params = {};
            params.value1 = "test";
            params.value2 = "param";

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

        </script>
</head>
<body>
    <h1>Example</h1>
    <p>Upload File</p>
</body>
</html>
  • 1
    By coding what have you tried? Show us some code. – Dhanuka Jun 21 '15 at 06:13
  • @DhanukaLakshan i added html code and this is php code – Ben Mechlia Nadia Jun 21 '15 at 06:38
  • @Frank van Puffelen : i can't figure out your modification – Ben Mechlia Nadia Jun 21 '15 at 21:33
  • try uploading the image as a base64string. This is how I go about it in firebase so you can upload it just like very other string. – Sani Yusuf Jun 22 '15 at 00:35
  • i tried it as a base 64-string but it didn't work i wonder if there is any permission to add to config.xml about the upload ? my capture image code and transfert the image to a 64 base string worked very well the problem is about the upload if u could help me with the upload code i would be thankful @SaniYusuf – Ben Mechlia Nadia Jun 22 '15 at 01:29
  • Make sure the folder you are sending the pictures has write permissions. Also the uploaded file size can be higher than the maximum size you can upload in your apache server ( you can change it in php.ini file). Otherwise this could be a problem of php code. Try to see with Wireshark if the request to the server is actually done. – AshBringer Jun 22 '15 at 10:15
  • @BenMechliaNadia You basically send the string the same way you send a normal string. So you do a Firebase push to a path and you should see it appear. – Sani Yusuf Jun 22 '15 at 11:41
  • See also: [How can I view and store images in firebase?](http://stackoverflow.com/questions/13955813/how-can-i-view-and-store-images-in-firebase) – Kato Jun 22 '15 at 21:17

0 Answers0