【问题标题】:send a camera photo to server in ionic在离子中将相机照片发送到服务器
【发布时间】:2016-04-13 14:54:46
【问题描述】:

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

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

这是控制器

.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;
    }
  }
}])

【问题讨论】:

    标签: cordova camera ionic-framework


    【解决方案1】:

    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页面中使用它来显示所选图像。

    希望对您有所帮助,祝代码日快乐

    【讨论】:

    • 这是一个很好的答案imageURI包含图像的base64字符串为什么不将那个发送到服务器
    • 您可以将该图像 base64 发送到服务器,在服务器端,他们需要将 base64 转换为服务器端的图像。
    • 你能给我一个代码吗?我不明白什么是 base64 以及我们如何转换为它并发送它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2011-04-11
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多