使用 phonegap/cordova 将相机图像上传到服务器并访问服务器返回的数据
Uploading camera image to server using phonegap/cordova and access the data returned by server
我是 phone gap/cordova 和 PHP 的新手。我创建了一个 phone gap/cordova 应用程序来将相机拍摄的图像上传到 server.The 服务器根据一些时间戳代码唯一地重命名图像并且它必须 return 该图像名称返回到app.I 已经很好地上传了图片,但是当我试图提醒 return 返回的数据时,我需要取回 server.On 成功上传回显的图片名称数据服务器,它正在警告 [object object]。我需要检索 value.How 可以吗 possible.My 下面给出了客户端的代码。
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";
?>
问题是在设备中成功上传后,它正在提醒 'Done! message returned back is [object object]'。我需要 value.Please 帮助我。
我想出了答案myself.Just想和你分享all.I使用alert(JSON.stringify(r))。现在我知道返回的格式json object 。知道我很容易打印出图像名称。r.response 帮助我检索生成的图像名称。
我是 phone gap/cordova 和 PHP 的新手。我创建了一个 phone gap/cordova 应用程序来将相机拍摄的图像上传到 server.The 服务器根据一些时间戳代码唯一地重命名图像并且它必须 return 该图像名称返回到app.I 已经很好地上传了图片,但是当我试图提醒 return 返回的数据时,我需要取回 server.On 成功上传回显的图片名称数据服务器,它正在警告 [object object]。我需要检索 value.How 可以吗 possible.My 下面给出了客户端的代码。
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";
?>
问题是在设备中成功上传后,它正在提醒 'Done! message returned back is [object object]'。我需要 value.Please 帮助我。
我想出了答案myself.Just想和你分享all.I使用alert(JSON.stringify(r))。现在我知道返回的格式json object 。知道我很容易打印出图像名称。r.response 帮助我检索生成的图像名称。