2017-04-17 35 views
0

我想通過使用http.post 的PHP文件傳遞用戶名和form_data對象,當我只傳遞form_data它工作我的圖片上傳。但我想傳遞一些其他信息,如用戶名。請幫助我如何通過http.post 其他數據這裏是我的PHP文件。

<?php include "connectdb.php"; $data=json_decode(file_get_contents("php://input")); 

$name=$dbhandle->real_escape_string($data->susername); if(!empty($_FILES)){ $date=2; 

$path = 'fooditem/'. $_FILES['file']['name']; 
if(move_uploaded_file($_FILES['file']['tmp_name'],$path)){ 

$query="INSERT INTO `login`(`id`,`type`,`img`) VALUES('".$name."','".$date."','".$_FILES['file']['name']."')"; 

    if($dbhandle->query($query)){ 
     echo 'File Uploaded'; 
    } 
    else 
     echo 'File Uploaded But Not Saved'; 
}}else{ 
echo 'Some Error';} 

myapp.directive("fileInput",function($parse){ 
 
      return{ 
 
       link: function($scope,element,attrs){ 
 
        element.on("change",function(event){ 
 
         var files = event.target.files; 
 
         $parse(attrs.fileInput).assign($scope, element[0].files); 
 
         $scope.$apply(); 
 
         // console.log(files[0].name); 
 
        }); 
 
       } 
 
      } 
 
     }); 
 
     
 
     
 
     
 
     myapp.controller("myController",function($scope,$http){ 
 
      
 
      
 
      $scope.signup = function(){ 
 
       
 
      var form_data = new FormData(); 
 
       angular.forEach($scope.files,function(file){ 
 
        form_data.append('file',file); 
 
       }); 
 
      $http.post("picupload.php",{'susername':$scope.susername,form_data}) 
 
        .success(function(response){ 
 
        alert(response); 
 
\t \t })     
 
      } 
<input type="text" ng-model="username" name="username"> 
 
<input type="file" file-input="files" accept="image/*" /> 
 
<input type="submit" value="SIGN UP" ng-click="signup()" name="signup_btn" class="btn btn-primary">

+0

是ES6嗎?如果是的話,你應該在$ _POST在php中看到你的「form_data」,否則你應該{'susername':$ scope.susername,「form_data」:form_data}並且它將在$ _POST – Leguest

回答

3

您可以添加這樣的事情:

myapp.controller("myController",function($scope,$http){ 
     $scope.signup = function(){  
     var form_data = new FormData(); 
     angular.forEach($scope.files,function(file){ 
       form_data.append('file',file); 
     }); 
     form_data.append('susername',$scope.susername); // new line 
     var config = {headers: { 'Content-type': undefined } }; 
     $http.post("picupload.php",form_data, config) 
       .success(function(response){ 
       alert(response); 
     });     
} 

我不知道PHP,但谷歌搜索後,我發現,在PHP 'susername' 可以檢索如下:

$_POST['susername']; 
+0

並且如何獲取susername是php文件? –

+0

我不確定PHP,但谷歌搜索後,我發現在PHP'susername'可以檢索如下: $ _POST ['susername'];試試看。 – CodingFreak

+0

是的,我已經在搜索它。 –

2

發佈由FormData API創建的對象時,必須將Content-type標頭設置爲undefined

$scope.signup = function(){ 

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

    form_data.append('susername', $scope.susername); 

    var config = {headers: { 'Content-type': undefined } }; 

    return $http.post("picupload.php",form_data, config) 
     .then(function(response){ 
     console.log(response.data); 
     return response.data; 
    });     
}; 

也是一個FormData object不能序列化爲JSON string它必須由單獨XHR API發送。追加所有必要的數據到FormData對象。

XHR send API發貼的FormData API創建的對象,它會自動將內容類型設置頭multipart/form-data具有適當encapsulation boundary並使用base64 encoding編碼數據。

通常,$http service覆蓋XHR API將內容類型標頭設置爲application/json。將內容類型標題設置爲undefined允許XHR API自由地正確設置標題。

+0

以及如何通過$用戶名 –

+0

查看更新的答案。 – georgeawg

+0

我嘗試$ _post ['susername']來獲取數據,它的工作原理 –

相關問題