如何从PhoneGaps文件传输API获取POST数据 [英] How to retrieve POST data from PhoneGaps File Transfer API

查看:118
本文介绍了如何从PhoneGaps文件传输API获取POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Phone Gaps (Cordova 2.1) file transfer API to post an image from the users photo library to my server. The file transfer API seems to be working fine. I'm just puzzled about retrieving this information on my server.

理想情况下,我需要做的是检索图像,然后将其上传到我的服务器。

Ideally, what I need to do is retrieve the image then upload it to my server. However, I can't seem to retrieve any information from the file transfer?

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

My JavaScript code (posting image data) is:

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"

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

All I'm getting is undefined variable errors?

任何帮助将非常感谢,谢谢!

Any help would be much appreciated, thanks!

推荐答案

http://some.server.com 您可以使用 / var / www / 目录中,在此目录中,您需要upload.php,并且此目录中的代码应将您的图片移动到文件夹

On http://some.server.com you can have your /var/www/ directory, in this directory you need upload.php and the code in this directory should move your image to the folder

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

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

That is the only extra thing you need.

先决条件:

XAMPP LAMP WAMP或MAMP,位于您的 http://some.server .com

XAMPP LAMP WAMP or MAMP on your http://some.server.com

请接受/ upvote如果有帮助。

Please accept/upvote if this helps.

为了清楚起见, javascript和html只是为了告诉你我的upload.php文件是否适合:
在你的头

For clarity, this is the javascript and html just to show you how my upload.php file fits in: In your head

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

在我身体中

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

这篇关于如何从PhoneGaps文件传输API获取POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆