FormData is empty after adding

Everyone that I use Ionic with, I try to send files to my server, but I have a problem with the form data, which is still empty after adding, when I check it on the console. I think that I found it empty and the data was not inserted into my DB.

PS: I use a lot of input files.

This is the controller:

.controller('perProfil', function($scope, annonceService, TDCardDelegate, $timeout, $http, $rootScope) {
  $scope.data = {}

  $scope.uploadedCin = function(element) {
    $scope.$apply(function($scope) {
      var files = element.files;
      $rootScope.cin = files[0];
    })
  }

  $scope.uploadedbultin = function(element) {
    $scope.$apply(function($scope) {
      var files = element.files;
      $rootScope.bult = files[0];
      $rootScope.FormData.append("bult", files[0]);
    })
  }

  $scope.uploadedavis = function(element) {
    $scope.$apply(function($scope) {
      var files = element.files;
      $rootScope.avis = files[0];
    })
  }

  $scope.uploadedCertif = function(element) {
    $scope.$apply(function($scope) {
      var files = element.files;
      $rootScope.certif = files[0];
      console.log($rootScope.certif);
    })
  }

  $scope.uploadedFile = function(element) {
    var url = 'http://localhost/documents?token=' + localStorage.getItem("token");

    console.log($rootScope.FormData);
    var fd = new FormData(this);

    fd.append("file", JSON.stringify($rootScope.cin));
    fd.append("butin", $rootScope.bult);
    fd.append("avis", $rootScope.avis);
    /fd.append("certifScol",$rootScope.certif);

    var data = {
      "idUser": "name",
      "type": "type"
    }

    fd.append("data", JSON.stringify(data));
    console.log(fd);

    $http.post(url, fd, {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
        transformRequest: angular.identity
      })
      .success(function(data) {
        console.log(data);
      })
      .error(function(data) {
        console.log(data);
      })
  }
})
+7
source share
1 answer

When registering a formData object only with console.log (formData), it always returns empty, since you cannot register formData. Please share your HTML too

0
source

Source: https://habr.com/ru/post/1656403/


All Articles