2013-02-27 70 views
2

我一直试图通过PhoneGap(科尔多瓦)设置一个应用程序来拍摄图像并将其上传到我们的服务器。我在这里经历了很多回复,并尝试了其中的代码。我可以拿起相机并拍照,甚至可以访问手机图库。但我无法将它发送到服务器。我试过发送图像,甚至发送base64图像流。我无法获得它到服务器。图像捕获/上传与Phonegap(科尔多瓦)iPhone不能正常工作

这里是在客户端的JavaScript:

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 

} 

function ImageUpload() { 
    this.useExistingPhoto = function(e) { 
     this.capture(Camera.PictureSourceType.SAVEDPHOTOALBUM); 
    } 

    this.takePhoto = function(e) { 
     this.capture(Camera.PictureSourceType.CAMERA); 
    } 

    this.capture = function(sourceType) { 
     navigator.camera.getPicture(this.onCaptureSuccess, this.onCaptureFaile, { 
      destinationType: Camera.DestinationType.FILE_URI, 
      soureType: sourceType, 
      correctOrientation: true 
     }); 
    } 

    this.onCaptureSuccess = function(imageURI) { 
     var fail, ft, options, params, win; 

     success = function(response) { 
      alert("Your photo has been uploaded!"); 
     }; 

     fail = function(error) { 
      alert("An error has occurred: Code = " + error.code + "\nMessage = "+error.message); 
     }; 

     options = new FailUploadOptions(); 
     options.fileKey = "file"; 
     options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); 
     options.mimeType = "text/plain"; 
     params = { 
      val1: "some value", 
      val2: "some other value" 
     }; 
     options.params = params; 
     ft= new FileTransfer(); 
     ft.upload(imageURI, 'http://style.appdev01.com/app/client-profile.php', success, faile, options); 
    } 

    this.OnCaptureFail = function(message) { 
     alert("Failed because: "+message); 
    } 
}; 
var imageuploader = new ImageUpload(); 

两个按钮调用imageuploader.takePhoto及.useExistingPhoto上点击。

在服务器端,我有这个PHP: 如果(isset($ _ FILES [ '文件'])){

$target_path = "/home/style/public_html/images/client_images/app_image.jpg"; 

move_uploaded_file($_FILES['file']['tmp_name'], $target_path); 

$insert = "INSERT INTO 
    `fut` 
SET 
    `request` = '".serialize($_POST)."', 
    `file` = '".serialize($_FILES)."'"; 
$mysql->query($insert); 

}

这只是存储POST和FILE阵列到数据库,以确保他们通过并创建图像。

但是,没有什么可以到服务器。任何帮助将不胜感激。我已经从这里和遍布网络的许多问题尝试了很多版本的代码。

回答

0
define ('SITE_ROOT', realpath(dirname(__FILE__))); /* echo SITE_ROOT; to dir 
move_uploaded_file($_FILES["file"]["tmp_name"],SITE_ROOT."/uploads/".$_FILES["file"]["name"]); // will move file, make sure uplaods has write permission! 

这对我的作品在Android模拟器,而不是平板电脑,但让我知道,如果你有工作,忙着同样的事情。

$myarray = array($_REQUEST); 
foreach ($myarray as $key => $value) { 

    echo "<p>".$key."</p>"; 
    echo "<p>".$value."</p>"; 
    echo "<hr />"; 
} 

你可以用它来检查POST/GET!

0

试试这是我的代码。它为我工作。

  1. 编码您的URL由是encodeURI方法
  2. 的FileKey以 “文件” 在你的服务器端脚本$ _FILES [ '文件']

    uploadFile: function(refNo){ 
    var uri = fileUpload.fileUri; 
    var file = uri.substr(uri.lastIndexOf('/') + 1); 
    
    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    options.fileName = file; 
    options.mimeType="image/jpeg"; 
    alert("name === "+uri); 
    options.chunkedMode = false; 
    
    
    var ft = new FileTransfer(); 
    Common.ajaxLoading('show'); 
    
    
    ft.upload(uri,encodeURI("http://172.16.1.147:80/upload/home.php") , fileUpload.uploadSuccess, fileUpload.uploadFail, options, true); 
    }, 
    
相关问题