2016-04-20 70 views
0

angularjs中的新手。我有一個表單,其中一個輸入字段具有類型文件。剩餘的全部是文本和選擇字段。我想,當我提交表格時,包括文件數據在內的所有表格數據應該一起發佈。這是我的代碼,請查看。Angular JS無法將文件數據與表格數據一起發送

// HTML

<input type="file" name="student_image" onchange="angular.element(this).scope().uploadFile(this.files)" ng-model="formData.studentImage" id="student_image"> 

// JS

var from_data = new FormData(); 
$scope.uploadFile = function (files) { 
     from_data.append("file", files[0]); 
}; 
$scope.submit = function() { 
     from_data.append('fullname',$scope.formData.studentFullname); 
     from_data.append('fullname',$scope.formData.studentEmail); 
     from_data.append('class',$scope.formData.studentClass); 
     $http({ 
      method: 'POST', 
      url: 'process.php', 
      enctype: 'multipart/form-data', 
      processData: false, 
      data: from_data, // pass in data as strings 
      headers: {'Content-Type': false} // set the headers so angular passing info as form data (not request payload) 
     }).success(function (data) { 
      console.log(data); 
     }); 
    }; 

回答

0

沒有enctypeprocessData性能。您還需要阻止JSON序列化。以下應該工作:

$http({ 
     method: 'POST', 
     url: 'process.php', 
     data: from_data, 
     headers: {'Content-Type': undefined}, 
     transformRequest: angular.identity, //to prevent JSON serialization 
    }