图像没有从角度上传到Laravel

时间:2015-10-09 06:05:21

标签: php angularjs laravel-5.1

我创建了一个用于保存项目详细信息的表单。我正在使用laravel 5.1和angularJS。项目信息将成功保存,但图像未上传到服务器,图像细节也未保存在数据库中。我需要帮助将图像上传到laravel使用angularjs

我的HTML代码:

app.controller('itemController',function($scope,$http,Item) {
$scope.items=[];
$scope.newitem={};
$scope.curitem = {};
$scope.sendmessage=false;

loadData();

//To Send Message to Site Admin..................

$scope.addItem=function(){
    if($scope.curitem.id){
        //TODO error display
        $scope.newitem.$update(function () {
            angular.extend($scope.curitem,$scope.curitem, $scope.newitem);
            $scope.sendmessage=true;
        });
    }else{
        $scope.newitem.$save(function (item) {
            //TODO error display
            $scope.items.push(item);
            $scope.sendmessage=true;
        });
    }
    //$scope.sendmessage=true;
    $scope.curitem = {};
    $scope.newitem = {};
    $scope.newitem=new Item();
    loadData();
};



function loadData(){
    var items =Item.query(function () {
        $scope.items = items;
        $scope.newitem=new Item();
    });
}
});


angular.module('ItemService',[]).factory('Item',['$resource',
function($resource){
    return $resource('/api/items/:itemId',{
        itemId:'@id'
    },{
        update:{
            method:'PUT'
        }
    });
}
]);

AngularJs代码:

        <?php

        namespace App\Http\Controllers\Auth;

        use App\File;
        use App\Item;
        use Illuminate\Http\Request as Request;
        use App\Http\Requests;
        use App\Http\Controllers\Controller;
        use Illuminate\Http\Response;
        use Validator;
        use Input;

       class ItemController extends Controller
       {
              /**
             * Validates given data for account
 * @param array $data
 * @return Validator
 */

protected function validator(array $data)
{
    // Loan account is not validated

    return Validator::make($data,[
        'name'  => 'required',
        'size'   => 'required|max:255'
    ]);

}


/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $validator = $this->validator($request->all());
    if($validator->fails()){
        return Response::json( $validator->errors()
            ,400);
    }
    $item=new Item($request->all());
    if($item->save())
    {
        $id=$item->id;
        if ($request->hasFile('photo')) {
            $file = $request->file('photo');
            $image=new File();
            $image->name = $this->uploadImage($file);
            $image->file_id=$id;
            $image->save();
        }
    }

    return Response::json(['error' => 'Server is down']
        ,500);
}


/**
 * To Strore Images
 *
 *
 */

public  function uploadImage($file){

        $storedFileName="";
        if(!empty($file)){

            $extension=$file->getClientOriginalExtension();
            $fileName = rand(11111,99999).'.'.$extension;
            $storedFileName=base_path().'/img/works/'. $fileName;
            $file->move(
                base_path().'/img/works/', $fileName
            );
        }
    return $storedFileName;
}


}

下面给出了ItemController.php ......

{{1}}

1 个答案:

答案 0 :(得分:3)

要通过AngularJS上传图像,您需要做一些额外的工作。你可以使用像nervgh/angular-file-upload(或其他一些库)这样的库,也可以自己动手。如需进一步参考,请观看this tutorial by egghead