0

I am developing my mobile application using ionic framework and I want it to connect to my API through ajax. Currenty, in the mobile side, which is I am using Ionic Framework, I want to upload an image and pass it to my api through ajax. I am using Cordova for the upload but it seems it doesn't found the URL I indicated.

Here's the HTML

<ion-footer-bar class="bar bar-positive">
  <div class="button-bar">
    <button class="button icon-left ion-upload" ng-click="uploadImage()" ng-disabled="image === null">Upload</button>
  </div>
</ion-footer-bar>

Here's the uploadImage() function in the controller (Just copied the code in a site. Forgot where) EDIT: added targetPath

$scope.uploadImage = function() {
    // Destination URL
    var url = "http://192.168.0.19/identificare_api/public/api/plants/image";
   var targetPath = $scope.pathForImage($scope.image);
    // File name only
   var filename = $scope.image;

   var options = {
       fileKey: "file",
       fileName: filename,
       chunkedMode: false,
       mimeType: "multipart/form-data",
       params : {'fileName': filename}
    };

    $cordovaFileTransfer.upload(url, targetPath, options).then(function(result) {
    var jsonparse = JSON.parse(result);
    $scope.showAlert(jsonparse);
}

But in the upload part, I want to do it in ajax to indicate the method for the URL but the problem I don't know what put in data.

$.ajax({
     url: "http://192.168.0.19/identificare_api/public/api/plants/image",
     type: 'POST',
     data: 
     success:function(json){
       var jsonparse = JSON.parse(json);
       alert(jsonparse);
     },
     error:function(){
         alert("Error");
     }
});

Can someone help me with this issue?

UPDATE: Applied here @Blauharley's comment below I had another issue here. I returned the $_FILES['image']['tmp_name'] in the API side but it returns nothing but when I returned the $_FILES['image']['name'], it returned my_image.jpg. Why it doesn't have tmp_name?

$scope.uploadImage = function() {

// File for Upload
var targetPath = $scope.pathForImage($scope.image);

$scope.getBase64ImageByURL(targetPath).then(function(base64Image){
  var blob = $scope.base64ToBlob(base64Image,'image/jpeg');
  var fd = new FormData();
  fd.append('image', blob, "my_image.jpg");
  fd.append('user_token', "rLUrh37rfTozuBxmemHtlKMgH");

  $.ajax({
    url: 'http://192.168.0.19/identificare_api/public/api/plants/image',
    type: 'POST',
    data: fd,
    contentType: false,
    processData: false,
    success:function(res){
      alert(res);
    },
    error:function(err){
      alert("Something's wrong with your api. Come on fix it!");
    }
  });
 });
};

$scope.getBase64ImageByURL = function(url) {
      var dfd = new $.Deferred();
      var xhr = new XMLHttpRequest();
      xhr.responseType = 'blob';
      xhr.onload = function() {
         var reader = new FileReader();
         reader.onloadend = function() {
             dfd.resolve(reader.result);
         }
         reader.readAsDataURL(xhr.response);
      };
      xhr.open('GET', url);
      xhr.send();
      return dfd.promise();
};

$scope.base64ToBlob = function(base64Image,toMimeType) {
      var byteCharacters = atob(base64Image.replace('data:'+toMimeType+';base64,',''));
      var byteNumbers = new Array(byteCharacters.length);
      for (var i = 0; i < byteCharacters.length; i++) {
            byteNumbers[i] = byteCharacters.charCodeAt(i);
      }
      var byteArray = new Uint8Array(byteNumbers);
      var blob = new Blob([byteArray], {
          type: toMimeType
      });
 return blob;
};

ADDED: API side

public function image(){
      echo json_encode($_FILES['image']['tmp_name']);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Carriane Lastimoso
  • 233
  • 1
  • 2
  • 7
  • I think you need to to give local URI. Have a look at https://gist.github.com/muhafazakar/29fc55bd156bcd509007 – Shakir Khan Jan 25 '17 at 18:18
  • Oh sorry. I forgot about it. I edited the `uploadImage()` above – Carriane Lastimoso Jan 25 '17 at 18:49
  • did u get your answer? – Shakir Khan Jan 25 '17 at 18:52
  • No, not yet. I already have that one in my code. I just don't know why it wasn't included in the code above. – Carriane Lastimoso Jan 25 '17 at 19:01
  • First of all is your server set up correctly? I do not reach it by using your url. Secondly you can use a blob-object to store your image-data into, as its done [here](http://stackoverflow.com/q/41398692/2401386) – Blauharley Jan 25 '17 at 19:26
  • Thank you @Blauharley. I applied your comment but having some issue again. I updated the question above indicating my issue. Why is it empty? – Carriane Lastimoso Jan 26 '17 at 03:46
  • You are welcome. This error might be a result when max size is exceed, as its states [here](http://stackoverflow.com/q/30358737/2401386) – Blauharley Jan 26 '17 at 11:15
  • Wow! Thank you very much. Great help! – Carriane Lastimoso Jan 26 '17 at 17:48
  • We do not put solutions in answers here, nor mark the title with [solved]. Would someone on this thread kindly add an answer post below? Please do not refer to comments above, as they can be deleted - please copy whatever content you need so it is in the answer. Thanks! – halfer Feb 01 '17 at 00:34

0 Answers0