0

Я хочу загрузить файл и другие данные с помощью angularjs. Я использую FormData, но получаю пустой массив со стороны сервера.Загрузка файла с другими данными в angularjs с laravel

Это моя форма

<form ng-submit="beatSubmit()" name="beatform" enctype="multipart/form-data"> 
    <input type="text" id="beat-name" ng-model="beatData.title" required="required" /> 
    <input type="file" id="image" file-model="image" /> 
    <input type="file" id="tagged_file" file-model="tagged_file" accept="audio/mp3" /> 
    <input type="file" id="untagged-beat" file-model="untagged_file" accept="audio/mp3" /> 
    <input type="text" class="form-control" id="price1" ng-model="beatData.price1"> 
</form> 

Вот мой контроллер и директива FileModel

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]); 
       }); 
      }); 
     } 
    }; 
    }]); 
// This is controller part in another file 
    $scope.beatSubmit = function(){ 

      var image = $scope.image; 
      var tagged_file = $scope.tagged_file; 
      var untagged_file = $scope.untagged_file; 

      var response = BEAT.uploadBeat($scope.beatData,image,tagged_file,untagged_file); 
      response.success(function(response){ 
       console.log(response); 
      }); 
     } 

И это моя служба

uploadBeat:function(data,image,tagged_file,untagged_file){ 
      var fd = new FormData(); 
      fd.append('image', image); 
      fd.append('tagged_file', tagged_file); 
      fd.append('untagged_file', untagged_file); 

      angular.forEach(data, function(value, key) { 
       fd.append(key,value); 
      }); 
      console.log(fd); // fd is null , I don't know why? 
      var req = { 
         method: 'POST', 
         transformRequest: angular.identity, 
         url: 'api/upload_music', 
         data: fd, 
         headers:{ 
          'Content-Type': undefined, 
          } 
         } 
      return $http(req); 

     } 

Когда я пытаюсь получить эти данные со стороны сервера, он вернет null. Я потратил больше времени на это, но у меня не было никакого решения. Если кто-нибудь знает, пожалуйста, помогите мне. Заранее спасибо.

ответ

0

Добавить это деталь заголовка к $ HTTP

$http({ 
        method: 'POST', 
        url: '', 
        headers: { 
           'X-Requested-With': 'XMLHttpRequest', 
           'Accept':'*/*', 
           'Content-type':'application/x-www-form-urlencoded;charset=utf-8' 
          }, 
        params:data, 
        timeout:4000 
        }); 

Laravel не разрешено подавать АЯКС запроса без того же политиков домена

0

Я нашел ошибку, я должен удалить ENCTYPE = "многочастный/form- данных "из формы.

Смежные вопросы