2014-01-23 18 views
1

将图像上传到Parse时出现问题我试图在用Cordova的camera.getPicture方法捕获图像后将图像保存到Parse。使用cordova的camera.getPicture

下面是运行此示例代码:

// onclick event to take a picture 
<button onclick="capturePhoto();"></button> 

// function implementation 
function capturePhoto() { 
    // Take picture using device camera and retrieve image as base64-encoded string 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, 
     destinationType: destinationType.DATA_URL 
    }); 
} 

// Success callback 
function onPhotoDataSuccess(imageData) { 
    var parseFile = new Parse.File("myPic.jpg", {base64:imageData}); 
    parseFile.save().then(function() { 
     // The file has been saved to Parse.     
    }, function(error) { 
     // The file either could not be read, or could not be saved to Parse. 
    }); 
} 

如果我简单地设置一个字符串,如在对JavaScript它的工作原理documentation

var base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE="; 
var file = new Parse.File("myfile.txt", { base64: base64 }); 

有人可能会指出我在正确的方向或给出一个备用的实施。

谢谢。

回答

1

你仍然需要在发送关闭之前预先考虑一些文件信息imageData

var base64pic = "data:image/jpeg;base64," + imageData; 
var parseFile = new Parse.File("myPic.jpg", {base64:base64pic}); 
+0

谢谢你这么这么这么多! –

0

您可以使用REST API来从科尔多瓦应用上传文件。

在你的控制器:

var cameraOptions = { 
    destinationType: 0, // base64 
    encodingType: 0 
}; 

$scope.takePicture = function() { 
    cameraOptions.sourceType = 1; 
    navigator.camera.getPicture(onSuccess, onFail, cameraOptions); 
} 

$scope.selectPicture = function() { 
    cameraOptions.sourceType = 0; 
    navigator.camera.getPicture(onSuccess, onFail, cameraOptions); 
} 

function onSuccess(picture) { 

    File.upload(picture) 
     .success(function(data) { 
      // upload finish 
     }); 

    $scope.$apply(function() { 
     $scope.preview = 'data:image/jpeg;base64,' + picture; 
    }); 
} 

function onFail(resp) { 
    alert('Error: ' + resp); 
} 

在文件服务:

angular.factory('File', function ($http) { 
    return { 
     upload: function (photo) { 

      var json = { 
       'base64': photo, 
       '_ContentType': 'image/jpeg' 
      } 

      var config = { 
       method: 'POST', 
       url: 'https://api.parse.com/1/files/pict.jpg', 
       data: json, 
       headers: { 
        'X-Parse-Application-Id': 'XXXXX', 
        'X-Parse-REST-API-Key': 'XXXXX' 
       } 
      }; 

      return $http(config); 
     } 
    } 
}); 
相关问题