使用AngularJS,PHP和MySQL发布数据

I want to store an image as a blob into my database(MySQL) while using PHP Rest service, but I dont know how to do it. Here is my PHP code (I'm using Slim framework for PHP)

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() .'}}';

}

}

and this is my angular code (i'm using fileuploader right now.)

        .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);
            });

        };
    })

1个回答



对于角度应用程序,您可以使用$ upload服务的upload方法,如下所示:</ p>

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

,如下所述:< a href =“https://github.com/danialfarid/ng-file-upload”rel =“nofollow”> https://github.com/danialfarid/ng-file-upload </ p> \ n

然后在您的PHP服务中,您可以使用</ p>

move_uploaded_file($ _ FILES ['file'] ['tmp_name'],$ file_path获取文件 ); </ code> </ p>

它会将文件存储在您选择的路径中,然后您可以使用PHP对文件数据执行任何操作。</ p>
</ DIV>

展开原文

原文

For your angular application, you can use the upload method of the $upload service like this:

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

as described in here : https://github.com/danialfarid/ng-file-upload

Then on your service in PHP, you can get the file using

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

It will store the file on the path of your choice, then you can use PHP to do whatever you want with the file data.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐