Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormData empty after append

Everyone I'm using Ionic I try to send files to my Server, but I have a problem with form data its still empty after append when I check it at console the same think I found it empty and no data is inserted at my DB.

PS: I use many inputs files.

This is 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);
      })
  }
})
like image 846
ryoko Avatar asked Sep 30 '16 17:09

ryoko


People also ask

How does FormData append work?

append() The append() method of the FormData interface appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.

How get all values from FormData?

getAll() The getAll() method of the FormData interface returns all the values associated with a given key from within a FormData object. Note: This method is available in Web Workers.

What does FormData return?

FormData.get() Returns the first value associated with a given key from within a FormData object. FormData.getAll() Returns an array of all the values associated with a given key from within a FormData .

How do I append a file to FormData?

We use the append method of FormData to append the file, passed as a parameter to the uploadFile() method, to the file key. This will create a key-value pair with file as a key and the content of the passed file as a value.


1 Answers

As per the answer here:...

var formData = new FormData(form); for (var [key, value] of formData.entries()) { console.log(key, value);} //or console.log(...formData)

like image 94
xplorer1 Avatar answered Sep 19 '22 14:09

xplorer1