簡體   English   中英

如何使用 FormData API 發出 POST 請求

[英]How to make POST requests with the FormData API

當我只傳遞 form_data 時,我想使用 http.post 將用戶名和 form_data 對象傳遞給 php 文件,它可以用於我的圖片上傳。 但我也想傳遞一些其他信息,如用戶名。 請幫助我如何在 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})
      .then(function(response){
          console.log(response);
    })                
});        
<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">

您可以添加如下內容:

 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'];

如何使用 FormData API 發出 POST 請求

發布由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 對象不能序列化為JSON 字符串,它必須由XHR API單獨發送。 將所有必要的數據附加到 FormData 對象。

XHR 發送 API發布由FormData API創建的對象時,它會自動將內容類型標頭設置為具有適當封裝邊界的multipart/form-data並使用base64 編碼對數據進行編碼

通常$http 服務會覆蓋XHR API將內容類型標頭設置為application/json 將內容類型標頭設置為undefined允許 XHR API 自由地正確設置標頭。


更新

在服務器端使用:

$_POST['susername'];

接收數據項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM