【问题标题】:How to retrieve POST data from PhoneGaps File Transfer API如何从 PhoneGaps 文件传输 API 检索 POST 数据
【发布时间】:2012-12-01 09:01:13
【问题描述】:

我正在使用 Phone Gaps (Cordova 2.1) 文件传输 API 将图像从用户照片库发布到我的服务器。文件传输 API 似乎工作正常。我只是对在我的服务器上检索此信息感到困惑。

理想情况下,我需要做的是检索图像,然后将其上传到我的服务器。但是,我似乎无法从文件传输中检索任何信息?

我的 JavaScript 代码(发布图像数据)是:

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

我的服务器端代码是:

 $paramValue = $_POST['fileKey']; //Undefined variable
 $paramValue2 = $_POST['options']; //Undefined variable
$paramValue3 = $paramValue2['fileKey'] //Undefined variable

我也试过了:

//POST variable
$paramValue = $_POST['params'];
echo "Param Value1: " . $paramValue['value1']; //Should return "test"

我也试过了:

//POST variable
$paramValue = $_POST['options'];
echo "Param Value1: " . $paramValue['options']['params']['value1']; //Should return "test"

我得到的只是未定义的变量错误?

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: php cordova file-upload slim


    【解决方案1】:

    http://some.server.com你可以有你的/var/www/目录,在这个目录中你需要upload.php并且这个目录中的代码应该把你的图片移动到文件夹中

    /var/www/TEST/

    <?php
       print_r($_FILES);
       $new_image_name = "YEAH.jpg";
       move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/TEST/".$new_image_name);
    ?>
    

    这是你唯一需要的额外的东西。

    先决条件:

    http://some.server.com 上的 XAMPP LAMP WAMP 或 MAMP

    为了清楚起见,这是 JavaScript 和 HTML,只是为了向您展示我的 upload.php 文件如何适合: 在你的脑海里

    <script type="text/javascript" charset="utf-8">
    
    // Wait for PhoneGap to load
    document.addEventListener("deviceready", onDeviceReady, false);
    
    // PhoneGap is ready
    function onDeviceReady() {
        console.log("device ready");
        // Do cool things here...
    }
    
    function getImage() {
        // 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 = new Object();
        params.value1 = "test";
        params.value2 = "param";
    
        options.params = params;
        options.chunkedMode = false;
    
        var ft = new FileTransfer();
        ft.upload(imageURI, "http://some.server.com/TEST/upload.php", win, fail, options);
    }
    
    function win(r) {
        console.log("Code = " + r.responseCode.toString()+"\n");
        console.log("Response = " + r.response.toString()+"\n");
        console.log("Sent = " + r.bytesSent.toString()+"\n");
        alert("Code Slayer!!!");
    }
    
    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
    }
    
    </script>
    
    </head>
    

    这就是我身体里的东西

    <button onclick="getImage();">Upload a Photo</button>
    

    【讨论】:

    • 抱歉,已经离开一段时间了。我最终完全按照您上面的描述解决了这个问题。谢谢。
    • 面临一个问题--> 只得到这样的输出: [CDVFileTransfer requestForUploadCommand:fileData:] [Line 206] fileData length: 1988265 之后什么都没有发生。既没有得到任何响应,也没有上传。
    • @Jquery Ninja:你能告诉我如何用它发送其他文本数据。以及如何使用 php?
    【解决方案2】:

    要访问参数值,请直接将它们作为 POST 访问。例如

    服务器端:echo "Param Value1: " . $_POST['value1']; //will return "test" as output.

    【讨论】:

      【解决方案3】:

      另外一件经常被忽视的事情: 本节介绍客户端js文件:

      options.fileKey="file";
      

      必须在服务器端匹配这部分:

      $_FILES["file"]
      

      否则,您将收到一个不指向此事实的错误。 这对某些人来说可能听起来很明显,但它可以为其他人节省一两个小时的时间。

      【讨论】:

      • 这为我节省了数小时的错误修复时间。感谢您的发布,我永远不会发现这一点。
      猜你喜欢
      • 2021-02-08
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 2018-02-12
      相关资源
      最近更新 更多