Cordova 文件上传无法在 android 上运行

Cordova file upload not working on android

使用 cordova 媒体捕获和文件传输插件我尝试制作一个应用程序来录制视频并上传到服务器。录制视频后,我调用上传功能,它在成功回调函数中显示成功消息。但是没有文件上传到服务器。这是我的代码。

            $(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" });
                }

我的php密码是

<?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';
}
?>

您必须创建一个 FileUploadOptions 对象

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

然后在上传功能上使用它

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

您还可以更改

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

function(result) { 
    alert(result); 
} 

只是为了检查服务器是否告诉你一些事情。

如果您收到成功回调,问题应该出在 PHP 脚本上。

我用这个returns上传文件的url

<?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;

?>