将相机照片发送到离子服务器

时间:2016-04-13 06:25:42

标签: cordova camera ionic-framework

我在离子中构建了一个应用程序。这个应用程序使用Cordova的相机插件从相机拍摄照片,然后我需要将它发送到服务器“让服务器成为本地主机”。

我编写了打开相机并显示图像的代码,但我需要帮助将图像发送到服务器。

这是控制器

.controller('stuffCtrl',function($scope, Camera,$http, $cordovaFile,$cordovaFileTransfer) { 

// camera is service I built to use camera 


    //this function is called when user try to take a picture 
    $scope.getPhoto= function() {
    Camera.getPicture().then(function(imageURI) {
      console.log(imageURI);
      $scope.lastPhoto = imageURI;
    }, function(err) {
      console.err(err);
    }, {
      quality: 75,
      targetWidth: 320,
      targetHeight: 320,
      destinationType: Camera.DestinationType.FILE_URI,
      saveToPhotoAlbum: true
    });
  };

})

相机服务

angular.module('app.services')

.factory('Camera', ['$q', function($q) {

  return {
    getPicture: function(options) {
      var q = $q.defer();

      navigator.camera.getPicture(function(result) {
        // Do any magic you need
        q.resolve(result);
      }, function(err) {
        q.reject(err);
      }, options);

      return q.promise;
    }
  }
}])

1 个答案:

答案 0 :(得分:0)

Huthaifah Kholi,我们需要将图像转换为base64,之后我们将其发送到服务器,这是将图像发送到服务器的最佳方法,但是用于将图像发送到服务器的API应该重新启动base64然后将其解析为图像 你可以找到将图像转换为base64图像的插件: 在您的代码中添加此插件:

ionic plugin add com-badrit-base64

在您收到图片时使用此代码:

window.plugins.Base64.encodeFile(imageFilePath, function(base64){
        console.log('file base64 encoding: ' + base64);
    });

现在将此base64字符串发送到服务器,或者您可以在html页面中使用它来显示所选图像。

希望这会对你有所帮助,祝你有一个愉快的代码日

相关问题