mardi 20 décembre 2016

how to pass formData in angular to laravle 5.3 with form that conatin file upload?

I have this form in blade file of laravel.

<form class="form-horizontal" ng-submit="onSubmit()" method="POST" id='products'enctype="multipart/form-data" novalidate="novalidate">

    <input type="hidden" name="_token" value="IPcUrZpuTnJQE22gFYCuD9bWMz5q90eCVvrlmtY4">

        <div class="form-group" ng-class="">

            <label class="col-md-2 control-label ">Product Name</label>

            <div class="col-md-10">

                <input class="form-control" 
                       name="prod_name"  
                       size ="" 
                       ng-model='myMoudle.prod_name'
                       maxlength=""
                       type="text" 
                       required="required" 
                       value="">

            </div>

        </div>

        <div class="form-group" ng-class="">

            <label class="col-md-2 control-label ">Picture</label>

            <div class="col-md-10">

                <input class="form-control " 
                       name="prod_pic"  
                       ng-model='myMoudle.prod_pic'
                       type="file" 
                       file-input="files"   
                       value="">

            </div>

        </div>

        <div class="form-group" ng-class="">

            <label class="col-md-2 control-label ">Price</label>

            <div class="col-md-10">

                <input class="form-control " 
                       name="prod_price"  
                       ng-model='myMoudle.prod_price'
                       type="number" 
                       value="">
            </div>  

        </div>

    <button type="submit" class="btn btn-info btn-sm pull-left"><i class="fa fa-plus"></i></button>

</form>

My js code:

app = angular.module('myMoudle', []);

app.controller('myController', function ($scope, $http) {

$scope.names = [];

$scope.onSubmit = function () {

    var fd = new FormData();

    $http({
        method: 'POST',
        url: 'CreateProducts',
        headers: {
            "Content-Type": undefined
        },
        data: fd

    })

    .success(function (response) {
        $scope.names = response;
    })

    .error(function (response) {
        console.log(response);
    });
}
});

and my php controller function looks like this:

public function postCreateProducts(Request $request) {

    print_R($_FILES) ;

    echo $request->input('prod_name');

    $posts = Input::all();
    print_R($posts) ;

}

all that is shown is Array(), Array(). And not what I send in the form. I will be very glad to get some help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire