ngCordova file_uri到base64

时间:2015-07-07 03:55:43

标签: angularjs cordova ionic-framework cordova-plugins ngcordova

我尝试使用ngCordova的相机插件(http://ngcordova.com/docs/plugins/camera/)从我的iphone获取照片并将其发送到我的服务器。后端开发人员希望我将其编码为base64。我的数据模型看起来像这样

[{image_name:"foo", link: //base64 string}]

这就是我的代码看起来像

$scope.getPictureFromGallery = function() {  $cordovaCamera.getPicture(options).then(function(imageURI) {


     $scope.image = imageURI;


    }, function(err) {
      // error
    });

  };

1 个答案:

答案 0 :(得分:2)

文档声明您可以设置destinationType以获得正确的结果。

module.controller('PictureCtrl', function($scope, $cordovaCamera) {

  document.addEventListener("deviceready", function () {

    var options = {
      quality: 50,
      destinationType: Camera.DestinationType.DATA_URL, // <== HERE
      sourceType: Camera.PictureSourceType.CAMERA,
      allowEdit: true,
      encodingType: Camera.EncodingType.JPEG,
      targetWidth: 100,
      targetHeight: 100,
      popoverOptions: CameraPopoverOptions,
      saveToPhotoAlbum: false
    };

    $cordovaCamera.getPicture(options).then(function(imageData) {
      var image = document.getElementById('myImage');
      image.src = "data:image/jpeg;base64," + imageData; // <== HERE is how you create the actual string to send to server
    }, function(err) {
      // error
    });

  }, false);
});

我在这里有一个完整的工作示例https://github.com/aaronksaunders/dcww/blob/master/www/js/services.js#L39