【问题标题】:Problems with file upload in LaravelLaravel 文件上传问题
【发布时间】:2017-03-25 21:38:50
【问题描述】:

我有一个带有文件上传输入的简单表单,它不会转储文件输入值。

这是我的表单代码:

<!-- Modal -->
        <div class="modal fade" id="app-form" tabindex="-1" role="dialog" aria-labelledby="app-form-label">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="app-form-label">Ședință foto boudoir!</h4>
                    </div>

                    {!! Form::open(['url'=>'','method'=>'POST', 'files'=>true, 'id' => 'model-form']) !!}
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
                                    {!! Form::label('first_name', 'Nume') !!}
                                    {!! Form::text('first_name', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('first_name'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('first_name') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>

                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
                                    {!! Form::label('last_name', 'Prenume') !!}
                                    {!! Form::text('last_name', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('last_name'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('last_name') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('age') ? ' has-error' : '' }}">
                                    {!! Form::label('age', 'Vârsta') !!}
                                    {!! Form::text('age', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('age'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('age') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>

                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('sex') ? ' has-error' : '' }}">
                                    {!! Form::label('sex', 'Vârsta') !!}
                                    {!! Form::select('sex', ['M' => 'Masculin', 'F' => 'Feminin'], null, ['class' =>
                                    'form-control',
                                    'placeholder' =>
                        '-- Selectează --']) !!}
                                    @if ($errors->has('sex'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('sex') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                    {!! Form::label('email', 'Email') !!}
                                    {!! Form::email('email', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('email') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>

                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">
                                    {!! Form::label('phone', 'Telefon') !!}
                                    {!! Form::text('phone', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('phone'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('phone') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-12">
                                <div class="form-group{{ $errors->has('details') ? ' has-error' : '' }}">
                                    {!! Form::label('details', 'Detalii') !!}
                                    {!! Form::textarea('details', '', ['class' => 'form-control', 'rows' => 10]) !!}
                                    @if ($errors->has('details'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('details') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        {!! Form::label('image', 'Fotografii Reprezentative') !!}
                        {!! Form::file('image') !!}
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-warning card-2">Trimite Datele</button>
                    </div>
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
        <!-- /End Modal -->

我用 ajax 用这段代码来发帖:

<script>
    (function () {
        $('#model-form').submit(function (e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'store',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                data: new FormData(this),
                contentType: false,
                cache: false,
                processData:false,
                success: function (response) {
                    console.log($('#image').val());
                }
            });
        });
    })();
</script>

现在不要问我为什么要在 ajax 中设置 csrf-token 标头,这是另一个问题,但现在的问题是我在提交表单后执行了 $_POST 和 $_FILES 转储,我拥有所有除图像文件上传数据外的表单值。 我正在用这个把头发拉出来,所以请帮帮我!

【问题讨论】:

  • 为文件转储 $_POST?你的意思是$_FILES?您应该为您上传的文件转储 $_FILES 超全局文件
  • 是的,对不起,我做了 $_FILES 到。
  • 不,没有控制台错误。
  • 只要我得到除了图像之外的所有输入数据......你怎么看?
  • 您的代码对我来说很好用。我所做的只是将您的 html/js 复制并粘贴到视图中,然后将 Request::all() 转储到 Route::post('store') 的闭包中。您能否在 routes.php 中为Route::post('store', ... 发布您的内容以及任何相关的控制器功能(如果有)?

标签: php file laravel upload


【解决方案1】:

当您尝试以如下形式发送文件数据时,您应该使用enctype=multipart/form-data:

<form action="" method="POST" enctype="multipart/form-data">
</form>

您可以像这样在控制器中使用 laravel 的 dd() 方法转储输入:

dd(request()->all());

希望这会有所帮助!

【讨论】:

  • 谢谢!我使用了 dd(request)->all() 并且看到了文件...为什么不使用 var_dump($_FILES) ?!
  • 不知道,但是,干杯,为解决您的问题:D
猜你喜欢
  • 2014-04-29
  • 1970-01-01
  • 2014-11-20
  • 2020-04-28
  • 2014-10-08
  • 2010-10-17
相关资源
最近更新 更多