javascript - 从 AngularJS 上传文件到 Laravel

标签 javascript php jquery html angularjs

这两天我一直在苦思冥想。我似乎无法通过文件上传来获得一个简单的表格。您可以在下面看到我在我的网络应用程序中为该页面设置的 Controller 。

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;

            }

        });

    };

}]);

这是属于它的 View :

<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 错误。但是,如果我从 PHP Controller 中删除 $fileName = $input->getClientOriginalName();,它不会,所以这一定是错误所在。

最佳答案

如果您想上传一些东西,请在您的表单中添加“enctype = multipart/form-data”。

关于javascript - 从 AngularJS 上传文件到 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25835288/

相关文章:

javascript - 销毁 jquery 模态

php - 表单提交在 mysql 中显示空白条目

php - Sphinx 部分单词搜索,版本 2.2.4

javascript - 在 HTML 表单上设置 Enter 键提交

javascript - .on(click - 在增加分数时显示新图像 - jQuery

javascript - 如何更改图片标题的 CSS?

javascript - 带有 iFrame 的后退按钮

javascript - 异步功能不起作用

PHP |计算剩余天数但考虑闰年

jquery - Css 和 jquery 背景颜色变化