文件从AngularJS上传到Laravel

时间:2014-09-14 16:13:56

标签: javascript php jquery html angularjs

我已经在这两天坚持不懈了。我似乎无法获得一个文件上传工作的简单表单。下面你可以在我的webapp中看到我对这个页面的控制器。

myExampleApp.controller('ProfileSettingsController', ['$scope', '$window', '$http', function ($scope, $window, $http) {

    $scope.master = {};

    $scope.update = function(formData) {
        $scope.master = angular.copy(formData);

        $http({
            method  : 'POST',
            url     : '/photos',
            data    : $.param($scope.formData)
        })
        .success(function(data) {

            console.log(data);

            var response = data.split("-");

            if (response[0] == 'Success') {

                $scope.successMessage = response[1];
                $scope.showSuccessAlert = true;

            } else {

                $scope.failMessage = response[1];
                $scope.showFailAlert = true;

            }

        });

    };

}]);

这是属于它的观点:

<h1>Change your photo here:</h1>

<div ng-controller="ProfileSettingsController">

    <div class="alert alert-success" ng-show="showSuccessAlert">{{ successMessage }}</div>
    <div class="alert alert-fail" ng-show="showFailAlert">{{ failMessage }}</div>

    <form>

        <label for="image">Image:</label>
        <input type="file" ng-model="formData.image" name="image"/>

        <div class="form-group">
            <button class="btn btn-primary" ng-click="update(formData)">Save</button>
        </div>

    </form>

</div>

此帖子/photos发布到Route::resource('photos', 'PhotosController');,如下所示:

<?php

class PhotosController extends BaseController {

    public function store() {

        $input = Input::get('image');

        $fileName = $input->getClientOriginalName();

        $fileName = str_replace(" " , "_" , $fileName);

        $image = Image::make($input['image']->getRealPath());

        File::exists(public_path()) or File::makeDirectory(user_photos_path());

        $image->save(user_photos_path().$fileName);
        $width = $image->width();
        $height = $image->height();

        $measure = ($width > $height) ? $height : $width;

        $image->crop($measure, $measure);
        $image->save(user_photos_path().'large-'.$fileName);
        $image->resize(200, 200);
        $image->save(user_photos_path().'tn-'.$fileName);
        $image->blur(15);
        $image->save(user_photos_path().'blur-'.$fileName);

        Photos::firstOrCreate(array('userid' => Auth::id()));

        $lawly = Photos::where('userid', '=', Auth::id())->first();

        // change the attribute
        $lawly->fileName = 'large-'.$fileName;
        $lawly->thumbnailName = 'tn-'.$fileName;
        $lawly->title = Auth::user()->username;

        // save to our database
        $lawly->save();

        echo "Success-Your profile picture has been changed.";

    }

    public function create() {

        echo "wrong route";

    }

}

但它总是抛出500 error。但是,如果我从PHP控制器中删除$fileName = $input->getClientOriginalName();,它就不会,因此必须是错误所在的位置。

2 个答案:

答案 0 :(得分:1)

如果你想上传一些东西,请添加&#34; enctype = multipart / form-data&#34;在你的形式。

答案 1 :(得分:0)

假设文件正在正确上传。尝试将此行更改为:

 $input = Input::file('image');

和这一行:

 $image = Image::make($input);