如何使用angular js formdata发布json数据和文件

时间:2017-11-22 18:22:09

标签: javascript angularjs json

我尝试使用angular formdata发送json数据和单个文件,但它不起作用。我没有从我的服务器收到json。这是我的angularjs代码

angular
.module('app')
.directive('fileModel', ['$parse', function ($parse) {
    return {
       restrict: 'A',
       link: function(scope, element, attrs) {
          var model = $parse(attrs.fileModel);
          var modelSetter = model.assign;
          element.bind('change', function(){
             scope.$apply(function(){
                modelSetter(scope, element[0].files[0]);
             });
          });
       }
    };
 }]);

angular
.module('BossApp')
.controller('addresumeCtrl', addresumeCtrl);

function addresumeCtrl ($scope,ngDialog,$http) {

     $scope.resume = {};
     $scope.resume.educations = [];
     $scope.resume.experiences = [];

     $scope.create = function(){
       var uploadUrl = "/user/add-resume";
       var fd = new FormData();
      for(var key in $scope.resume)
         fd.append(key, $scope.resume[key]);
      $http.post(uploadUrl, fd, {
      transformRequest: angular.identity,
     headers: {'Content-Type': undefined}
     }).then(function (response) {

    }, function (error) {

     });
   };
 }

如何解决这个问题?

0 个答案:

没有答案