变量是未定义的phonegap

时间:2014-10-14 06:25:15

标签: cordova camera

我成功地从设备照片galery拍照并在我的应用程序中显示它,弹出窗口要求我确认图片,如果我点击确认图片应该通过文件服务器上传到服务器。我的问题是,当我点击弹出窗口的确认按钮时,控制台日志告诉我这个“imageData未定义”

<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

var pictureSource;   
var destinationType;
var callback;

var message = "Confirm your picture?";
var title = "Confirm your picture";

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

function onDeviceReady() {
  pictureSource=navigator.camera.PictureSourceType;
  destinationType=navigator.camera.DestinationType;
}

function onPhotoURISuccess(imageData) {

var smallImage = document.getElementById('smallImage');

smallImage.style.display = 'block';

smallImage.src =  imageData;

showConfirm(message, callback, buttonLabels, title);
 }

function getPhoto() {

  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
  destinationType: destinationType.FILE_URI,
  sourceType:  navigator.camera.PictureSourceType.PHOTOLIBRARY });
}

function onFail(message) {
  alert('Failed because: ' + message);
}

function uploadPhoto(imageData)
{

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageData.substr(imageData.lastIndexOf('/')+1);
options.mimeType="image/jpeg";

var params = {};
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(imageData, encodeURI("url"), 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);

} 

var buttonLabels = "Confirm,NO";

callback = function(Confirm)
{
 if(Confirm)
 {


uploadPhoto(imageData);


   alert('Confirm');

}else
{ 
   alert('delete'); 
}
};


function showConfirm(message, callback, buttonLabels, title)
{       
 buttonLabels = buttonLabels || 'OK,Cancel';

  title = title || "default title";


 if(navigator.notification && navigator.notification.confirm)
 {

       var _callback = function(index)
       {
           if(callback)
           {
               callback(index == 1);
           }
       };

       navigator.notification.confirm
       (
           message,      
           _callback,   
           title,        
           buttonLabels  
       );


   }else{
   invoke(callback, confirm(message));
  }
  }

</script>
</head>
<body>

<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>

<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
</body>
 </html>

我的问题在这里,当我点击按钮确认弹出窗口(确认将图片上传到服务器)时,我在控制台中显示此错误消息“imageData未定义”

callback = function(Confirm)
{
 if(Confirm)
 {

  uploadPhoto(imageData);// Here i get he error message in my console tellign me that imageData is undefined


   alert('Confirm');

 }else
 {
   alert('delete'); 
 }
};

1 个答案:

答案 0 :(得分:0)

尝试将imageData中的onDeviceReady定义为全局,如:window.imageData。我怀疑范围是问题所在。