简体   繁体   English

通过发布请求将图像从Ionic应用发送到Rails端点

[英]Sending an image via a post request from an Ionic app to a rails endpoint

I am currently working on an Ionic mobile application which will eventually take photos, attach a location and send them inside a post request to a rails endpoint. 我目前正在开发一个Ionic移动应用程序,该应用程序最终将拍照,附加位置并将其在发布请求中发送到Rails端点。 After looking at this link and this link and countless others, I have been unable to find any solid information on implementing this particular feature. 在查看了此链接和该链接以及无数其他链接之后 ,我无法找到有关实现此特定功能的任何可靠信息。

I can upload photos through the browser using a html input form, which is then added to the database and is displayed on the app via a get request. 我可以使用html输入表单通过浏览器上传照片,然后将其添加到数据库中,并通过get请求显示在应用程序上。

However at the moment when taking a photo on the phone and attempting to send it via a post request directly from the app, only the location information is being received, the image is not being correctly encoded. 但是,当在手机上拍照并尝试直接通过应用程序通过发布请求发送照片时,仅接收到位置信息,图像未正确编码。

Here is the jSON data that has been received, its returning "image_url":"/images/main/missing.png" . 这是已收到的jSON数据,返回的是"image_url":"/images/main/missing.png"

    { "id":6,"city":"Greater London",
      "country":"United Kingdom","created_at":"2015-05-14T21:22:22.825Z",
      "updated_at":"2015-05-14T21:22:22.825Z","image_file_name":null,
      "image_content_type":null,"image_file_size":null,
      "image_updated_at":null,"image_url":"/images/main/missing.png" }

Here is the code: 这是代码:

Angular factory making post request: 角工厂定购要求:

.factory('Posts', function($http) {

  var o = { posts: [] };

  o.getAll = function() {
    return  $http.get('http://localhost:8100/posts').success(function(data) {
      angular.copy(data, o.posts);
    });
  };

  o.addPost = function(post) {
    return $http.post('https://shielded-hamlet-4665.herokuapp.com/posts', post);
  };

  return o;
})

Angular Controller taking photo: Angular Controller拍照:

.controller("CameraCtrl", function($scope, $cordovaCamera, $http, Posts) {

  var id = 0;

  var options = { 
    quality : 75, 
    destinationType : Camera.DestinationType.FILE_URI, 
    sourceType : 1, 
    allowEdit : true,
    encodingType: 0,
    targetWidth: 380,
    targetHeight: 450,
    popoverOptions: CameraPopoverOptions,
    saveToPhotoAlbum: false
  };

  function getLocCoords(position) {
    $scope.lat = position.coords.latitude;
    $scope.lon = position.coords.longitude;

    $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + $scope.lat +',' + $scope.lon + '&sensor=true')
      .success(function(data) {

        var home = data.results[0].address_components;

        for (var i = 0; i < home.length; i++) {
          if(home[i].types.indexOf("administrative_area_level_2") > -1) {
            $scope.city = home[i].long_name;
            break;
          };
        };

        for (var i = 0; i < home.length; i++) {
          if(home[i].types.indexOf('country') > -1) {
            $scope.country = home[i].long_name;
            break;
          };
        };
      })
  };

  $scope.takePicture = function() {

    navigator.geolocation.getCurrentPosition(getLocCoords);

    $cordovaCamera.getPicture(options).then(function(imageData) {
      $scope.imgURI = imageData;
      id ++;
      var post = { id: id, country: $scope.country, city: $scope.city, image: $scope.imgURI, likes: 0, comments: [] }
      Posts.addPost(post);
    }, function(err) {

    });
  }

Post Controller from the Rails Database: Rails数据库中的Post Controller:

class PostsController < ApplicationController

  skip_before_filter :verify_authenticity_token

  def index
    @posts = Post.all
    render json: @posts, :callback => params['callback'], :content_type => 'application/javascript', :methods => [:image_url]
  end

  def new
    @post = Post.new
  end

  def create
    Post.create(post_params)
    redirect_to '/posts'
  end

  def post_params
    params.require(:post).permit(:city, :country, :image)
  end

end

I have not done a great deal of work with the ionic framework so please forgive my ignorance. 我在离子框架方面还没有做很多工作,所以请原谅我的无知。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Managed to solve this using the cordovaFileTransfer.upload method. 设法使用cordovaFileTransfer.upload方法解决了这一问题。

The rails end point was also filtering params and looking for a post object, with a image string, and only an image string was being provided. rails端点也正在过滤参数并寻找带有图像字符串的后置对象,仅提供了图像字符串。

The following code is now working 以下代码正在运行

Angular factory making post request: 角工厂定购要求:

.factory('Posts', function($http, $cordovaFileTransfer) {

  var o = { posts: [] };

  o.getAll = function() {
    return $http.get('https://shielded-hamlet-4665.herokuapp.com/posts').success(function(data) {
      angular.copy(data, o.posts);
    });
  };

  o.addPost = function(post) {

    var options = {
      fileKey: "image",
      fileName: "image.jpeg",
      chunkedMode: false,
      mimeType: "image/jpeg",
      params: { city: post.city, country: post.country, lat: post.lat, lon: post.lon }
    };

    $cordovaFileTransfer.upload('http://shielded-hamlet-4665.herokuapp.com/posts', post.image, options)
      .then(function(result){
        console.log("Code = ok");
      }, function(error){
        console.log("Code = " + error);
      }, function(progress){});
  };

  return o;
})

Angular Controller taking photo: Angular Controller拍照:

.controller("CameraCtrl", function($scope, $cordovaCamera, $http, Posts) {

  post = {};

  var options = { 
    quality : 75, 
    destinationType : Camera.DestinationType.FILE_URI, 
    sourceType : 1,
    allowEdit : true,
    encodingType: 0,
    targetWidth: 380,
    targetHeight: 450,
    popoverOptions: CameraPopoverOptions,
    saveToPhotoAlbum: false
  };

  function getLocCoords(position) {
    post.lat = position.coords.latitude;
    post.lon = position.coords.longitude;

    $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + post.lat +',' + post.lon + '&sensor=true')
      .success(function(data) {

        var home = data.results[0].address_components;

        for (var i = 0; i < home.length; i++) {
          if(home[i].types.indexOf("administrative_area_level_2") > -1) {
            post.city = home[i].long_name;
            break;
          };
        };

        for (var i = 0; i < home.length; i++) {
          if(home[i].types.indexOf('country') > -1) {
            post.country = home[i].long_name;
            break;
          };
        };
      })
  };

  $scope.takePicture = function() {

    navigator.geolocation.getCurrentPosition(getLocCoords);

    $cordovaCamera.getPicture(options).then(function(imageData) {
      post.image = imageData;
      Posts.addPost(post);
    }, function(err) {});
  };
});

Post controller from rails database: Rails数据库中的Post Controller:

class PostsController < ApplicationController

  skip_before_filter :verify_authenticity_token

  def index
    @posts = Post.all
    render json: @posts, :callback => params['callback'], :content_type => 'application/javascript', :methods => [:image_url]
  end

  def new
    @post = Post.new
  end

  def create
    Post.create(post_params)
    redirect_to '/posts'
  end

  def post_params
    params.permit(:city, :country, :image, :lat, :lon)
  end

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM