dou4121 2015-04-21 08:05
浏览 75

使用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条回答

  • duanhan9479 2015-04-21 08:22
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组