Search code examples
androidcordovaphonegap-pluginscordova-pluginsfile-transfer

Cordova file upload not working on android


using cordova media-capture and file-transfer plugin I try to make an app to record video and upload on server. After recording video I call the upload function and it showing the success message in success callback function. But no file uploaded in server. Here my code.

            $(document).ready(function(){
                document.addEventListener("deviceready", onDeviceReady, false);
            });

            function onDeviceReady() {
            navigator.device.capture.captureVideo(captureSuccess, captureError, {limit:1});
            }
            var captureSuccess = function(mediaFiles) {
                        var i, len;
                        for (i = 0, len = mediaFiles.length; i < len; i += 1) {
                            uploadFile(mediaFiles[i]);
                        }
                };

            var captureError = function(error) {
               alert('error');
               console.log(error);
            };

            /********* File Upload  **********/

            // Upload files to server
                function uploadFile(mediaFile) {
                    var ft = new FileTransfer(),
                        path = mediaFile.fullPath,
                        name = mediaFile.name;



                    ft.upload(path,
                        "http://myserver.com/test.php",
                        function(result) {
                            alert('success');
                        },
                        function(error) {
                           alert('error');
                        },
                        { fileName: name, fileKey: "file" });
                }

My php code is

<?php
if($_FILES['file']['size'] > 0) {
$path = $_SERVER['DOCUMENT_ROOT']."/audior/app/";
$tmpname1 = $_FILES['file']['tmp_name'];
$ptname1 = $_FILES['file']['name'];
move_uploaded_file($tmpname1, $path . $ptname1);

echo 'success';
}
?>

Solution

  • You have to create a FileUploadOptions object

    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = name;
    

    then use it on the upload function

    ft.upload(path, 
              "http://myserver.com/test.php",          
              function(result) {
                  alert('success');
              },
              function(error) {
                  alert('error');
              },
              options);
    

    You can also change the

    function(result) { 
        alert('success'); 
    }
    

    to

    function(result) { 
        alert(result); 
    } 
    

    Just to check if the server is telling you something.

    If you are getting the success callback the problem should be on the PHP script.

    I use this one that returns the url of the uploaded file

    <?php
    
    $target_dir = "upload/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    move_uploaded_file($_FILES["file"]["tmp_name"], "./" . $_FILES["file"]["name"]);
    echo "http://" . $_SERVER['SERVER_NAME'] . "/" . $target_file;
    
    ?>