Angular js文件上传问题

时间:2018-01-10 11:07:25

标签: javascript jquery angularjs

以下是我的控制器代码和上传文件的自定义指令:

angular.module('myApp', [])
.directive('fileModel', ['$parse', function ($parse) {
        return {
           restrict: 'A',
           link: function(scope, elemesnt, attrs) {
              element.bind('change', function(){
              $parse(attrs.fileModel).assign(scope,element[0].files)
                 scope.$apply();
              });
           }
        };
}])
.controller('myCtrl', ['$scope', '$http', function($scope, $http){
       $scope.uploadFile=function(){
       var fd=new FormData();
        console.log($scope.files);
        angular.forEach($scope.files,function(file){
        fd.append('file',file);
        });
       $http.post('http://192.168.1.161/www/ci_practice/assets/',fd,
           {
               transformRequest: angular.identity,
               headers: {'Content-Type': undefined}                     
            }).success(function(d)
                {
                    console.log(d);
                })         
       }
}]);

我无法将图像上传到assets文件夹。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

Meteor.methods({
  createTask(task) {
    // TODO Add a check here to validate
    var returnID;
    Tasks.insert(task, (err, id) => {
      if (err) {
        throw new Meteor.Error(err);
      }
      id = {id: id};
      returnID = id;
      console.log('Returning id: ', id);
      // return id; --not here
    });
   return returnID; //return it here.
  }
});