2015-04-21 46 views
1

我想在使用PHP Rest服务时将图像作为blob存储到我的数据库(MySQL)中,但我不知道如何去做。这是我的PHP代码(我使用PHP框架修身)使用AngularJS,PHP和MySQL发布数据

function addProblem() { 
global $app; 
$postdata = file_get_contents("php://input"); 
$req = json_decode($postdata); // Getting parameter with names 
$paramName = $req->station; // Getting parameter with names 
$paramAdres = $req->address; // Getting parameter with names 
$paramCity = $req->city;// Getting parameter with names 
$parampostal = $req->postalcode; 
$parampic = $req->pictureOfDamage; 
$paramdescrip= $req->description; 
$sql = "INSERT INTO problems (Station,Address,Postalcode,City,PictureOfDamage,Description) VALUES (:station,:address,:postalcode,:city,:pictureOfDamage,:description)"; 
try { 
    $dbCon = getConnection(); 
    $stmt = $dbCon->prepare($sql); 
    $stmt->bindParam(':station', $paramName); 
    $stmt->bindParam(':address', $paramAdres); 
    $stmt->bindParam(':city', $paramCity); 
    $stmt->bindParam(':postalcode', $parampostal); 
    $stmt->bindParam(':pictureOfDamage', $parampic); 
    $stmt->bindParam(':description', $paramdescrip); 
    $stmt->execute(); 
    $dbCon = null; 
    echo json_encode("toegevoegd "); 

} catch(PDOException $e) { 
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 

} 

}

,这是我的角度代码(我用fileuploader现在。)

 .controller('MeldingController', function ($scope, $upload, $rootScope, $state, $http) { 
     $scope.station = $rootScope.station; 
     $scope.PictureOfDamage; 
     $scope.upload = function (files) { 
      if (files && files.length) { 
       for (var i = 0; i < files.length; i++) { 
        var pictureOfDamage = files[i]; 
        return pictureOfDamage; 
       } 
      } 
     } 
     $scope.submit = function() { 
      console.log($scope.PictureOfDamage); 
      var data = { 
       station: $scope.station.name, 
       address: $scope.station.streetName, 
       postalcode: $scope.station.postalCode, 
       city: $scope.station.city, 
       pictureOfDamage: $scope.upload($scope.files) /* picture*/, 
       description: document.getElementById("Description").value 
      } 
      console.log('NOJSN ', data); 
      data = JSON.stringify(data); 
      console.log('JSON', data) 
      $http({ 
       method: "POST", 
       url: 'http://localhost/Dats24/problem/add/', 
       data: data}) 
        .success(function (data, status, headers, config) { 
         $state.go('GoogleMaps'); 
        }).error(function (data, status, headers, config) { 
       console.log(data); 
      }); 

     }; 
    }) 

回答

0

对于您的角度应用,您可以使用$ upload服务的上传方法,如下所示:

file_upload: function(file) { return $upload.upload({ url: ' http://your-upload-url/ ', file: file }); }

在这里所描述

https://github.com/danialfarid/ng-file-upload

然后在PHP你的服务,你可以使用

move_uploaded_file($_FILES['file']['tmp_name'], $file_path);

它将存储您所选择的路径上的文件的文件,那么你可以使用PHP用文件数据做你想做的任何事情。