在$ http POST中传递2个参数

时间:2016-12-13 16:47:37

标签: javascript php angularjs http post

我有一个场景,其中一个属性可以有多个图像,但该图像只能分配给一个属性(一对多,propertyId为FK)。

在html中我检索propertyId并使用uploadFile函数传递它

<div id="imageDiv" ng-controller="uploadImageCtrl">  
                 <input type="button" class="btn btn-info" value="Upload Image" ng-click="uploadFile(pro.id)"/>
                <input type="file" file-input="files"/>  
            </div>

这里我有我的app.js,我在那里检索图像,propertyId是Id。我试图传递id和form_data。

app.directive("fileInput", function($parse){  
  return{  
       link: function($scope, element, attrs){  
            element.on("change", function(event){  
                 var files = event.target.files;  
                 console.log(files[0].name);  
                 $parse(attrs.fileInput).assign($scope, element[0].files);  
                 $scope.$apply();  
            });  
       }  
  }  
 });  
 app.controller("uploadImageCtrl", function($scope, $http,$routeParams,$location){ 

  $scope.uploadFile = function(id){

     var form_data = new FormData();  
       angular.forEach($scope.files, function(file){  
        form_data.append('file', file);  
          }); 

    $scope.id = id;
    console.log(id);
    $routeParams.id = id;
    //$scope.activePath = null;
    $scope.updateMessage="";

    console.log(form_data);        
  $http.post('php/uploadImage.php/',{'id':id},form_data,  
       {  
            transformRequest: angular.identity,  
            headers: {'Content-Type': undefined,'Process-Data': false}  
       }).success(function(response){  
            alert(response);  

       });  
  }  
 });

然后在uploadImage.php中我将$ id分配给从app.j检索到的propertyId并将图像上传到该propertyId。

<?php

$data = json_decode(file_get_contents("php://input"));
$id=$data->id;
echo $id;
require_once("connection.php");
$conn = connectToDb();

if(!empty($_FILES))  
 {  
      $path = '../Images/' . $_FILES['file']['name'];  
      if(move_uploaded_file($_FILES['file']['tmp_name'], $path))  
      {  
           $insertQuery = "INSERT INTO tbl_images(imagePath , propertyId) VALUES ('".$_FILES['file']['name']."',$id)";  
           if(mysqli_query($conn, $insertQuery))  
           {  
                echo 'File Uploaded';  
           }  
           else  
           {  
                echo 'File Uploaded But not Saved';  
           }  
      }  
 }  
 else  
 {  
    echo  mysqli_error($conn);
 }
?>

问题是,它给我一个空白错误,数字8,不知道为什么不上传,8代表$ id(echo $ id)。所以propertyId是被成功检索并传入php。

错误可能是最后一个 ELSE 声明(echo mysqli_error($ conn))。

form_data是否未成功通过?

1 个答案:

答案 0 :(得分:0)

我只需要使用表单数据附加 id ,然后使用正常的 POST 检索ID。

app.controller("uploadImageCtrl", function($scope, $http,$routeParams,$location){ 

  $scope.uploadFile = function(id){

    var form_data = new FormData();  
    angular.forEach($scope.files, function(file){  
        form_data.append('file', file);  
    }); 
    form_data.append('elmID', id);

  $http.post('php/uploadImage.php',form_data,  
       {  
            transformRequest: angular.identity,  
            headers: {
                'Content-Type': undefined,
                'Process-Data': false
            }  
       }).success(function(response){  
            alert(response);
       });
  }

 });
相关问题