Php 使用phonegap/cordova将摄像头图像上传到服务器,并访问服务器返回的数据
我不熟悉电话gap/cordova和PHP。我创建了一个phone gap/cordova应用程序,用于将相机拍摄的图像上载到服务器。服务器根据一些时间戳代码对图像进行唯一重命名,并且必须将该图像名称返回到应用程序。我已经很好地上载了图像,但我需要检索服务器回显的图像名称数据。成功上载后,当我试图向服务器返回的数据发出警报时,它正在向[object object]发出警报。我需要检索该值。这怎么可能。我在客户端的代码如下所示Php 使用phonegap/cordova将摄像头图像上传到服务器,并访问服务器返回的数据,php,android,cordova,phonegap-plugins,Php,Android,Cordova,Phonegap Plugins,我不熟悉电话gap/cordova和PHP。我创建了一个phone gap/cordova应用程序,用于将相机拍摄的图像上载到服务器。服务器根据一些时间戳代码对图像进行唯一重命名,并且必须将该图像名称返回到应用程序。我已经很好地上载了图像,但我需要检索服务器回显的图像名称数据。成功上载后,当我试图向服务器返回的数据发出警报时,它正在向[object object]发出警报。我需要检索该值。这怎么可能。我在客户端的代码如下所示 var pictureSource; // picture sou
var pictureSource; // picture source
var destinationType; // sets the format of returned value
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
pictureSource = navigator.camera.PictureSourceType;
destinationType = navigator.camera.DestinationType;
}
function clearCache() {
navigator.camera.cleanup();
}
var retries = 0;
function onCapturePhoto(fileURI) {
var win = function (r) {
clearCache();
retries = 0;
alert('Done! message returned back is '+r);
}
var fail = function (error) {
if (retries == 0) {
retries ++
setTimeout(function() {
onCapturePhoto(fileURI)
}, 1000)
} else {
retries = 0;
clearCache();
alert('Ups. Something wrong happens!');
}
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var params=new param();
param.client_device_id=device.uuid;
options.params = params; // if we need to send parameters to the server request
var ft = new FileTransfer();
ft.upload(fileURI, encodeURI("http://host/upload.php"), win, fail, options);
}
function capturePhoto() {
navigator.camera.getPicture(onCapturePhoto, onFail, {
quality: 100,
destinationType: destinationType.FILE_URI
});
}
function onFail(message) {
alert('Failed because: ' + message);
}
下面给出了upload.php的代码
<?php
if (!file_exists($_POST["client_device_id"])) {
mkdir($_POST["client_device_id"], 0777, true);
}
$date = new DateTime();
$timeStamp=$date->getTimestamp() -1435930688;
move_uploaded_file($_FILES["file"]["tmp_name"], 'f:\\xampp\\htdocs\\FileUpload\\'.$_POST["client_device_id"]."\\".$timeStamp.'.jpg');
echo $timeStamp.".jpg";
?>
我自己找到了答案。只想与大家分享。我使用了alert(JSON.stringify(r))。现在我知道了返回的JSON对象的格式。知道我已经轻松打印出了图像名称。r.response帮助我检索生成的图像名称