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.

    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架