【问题标题】:Uploading camera image to server using phonegap/cordova and access the data returned by server使用 phonegap/cordova 将相机图像上传到服务器并访问服务器返回的数据
【发布时间】:2015-07-29 05:14:49
【问题描述】:

我是电话 gap/cordova 和 PHP 的新手。我创建了一个电话间隙/科尔多瓦应用程序来将相机拍摄的图像上传到服务器。服务器根据一些时间戳代码唯一地重命名图像,它必须将该图像名称返回给应用程序。我已经很好地上传了图像但我需要检索服务器回显的图像名称数据。成功上传后,当我尝试提醒服务器返回的数据时,它正在提醒 [object object]。我需要检索该值。如何才能有可能。我在客户端的代码如下。

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";
?>

问题是在设备中成功上传后,它会提示“完成!返回的消息是 [object object]'。我需要获取值。请帮助我。

【问题讨论】:

    标签: php android cordova phonegap-plugins


    【解决方案1】:

    我自己想出了答案。只是想和大家分享一下。我使用了alert(JSON.stringify(r))。现在我知道了返回的json对象的格式。知道我已经轻松打印出了图像name。r.response帮我找回了生成的图片名称。

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2018-01-19
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多