无法从Laravel中的请求中读取multipart / form-data

时间:2016-11-17 13:31:20

标签: ajax laravel multipartform-data form-data

我正在尝试使用AJAX发送带有上传文件和multipart / form-data编码的表单。我正在使用这种形式:

{!! Form::model($user, ['method' => 'PATCH', 'url' => ['/administrator/users', $user->id], 'class' => 'form-horizontal', 'files' => 'true', 'id' => 'userEdit']) !!}
    <div class="modal-body">
        <div class="form-group">
            <label class="col-sm-3 control-label">Avatar:</label>
            <div class="col-sm-9">
                <img src="/dashboard/assets/img/avatar/{{ $user->profile->avatar }}" class="img-circle m-b" />
                <input type="file" name="avatar" />
            </div>
        </div>
        <hr />
        <div class="form-group">
            <label class="col-sm-3 control-label">Name:</label>
            <div class="col-sm-9">
                <input type="text" name="first_name" value="{{ $user->profile->first_name }}" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">Surname:</label>
            <div class="col-sm-9">
                <input type="text" name="last_name" value="{{ $user->profile->last_name }}" class="form-control" />
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-info" data-user-id="{{$user->id}}">Save</button>
    </div>
{!! Form::close() !!}

我尝试使用Ajax发送数据:

$('#editUser').submit('#userEdit', function(event) {
            event.preventDefault();
            $.ajax({
                type: 'PATCH',
                url: '/administrator/users/1',
                data: new FormData(userEdit),
                processData: false,
                contentType: false,
                mimeType: "multipart/form-data",
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(data) {
                    alert(data);
                },
                error:  function(xhr, str){
                    alert(str);
                }
            });
        });

结果,我无法从控制器中的请求中读取输入。它返回一个空数组。

    public function update($id, Request $request){
        dd($request->all());
    }

我认为我在发送多部分数据方面做错了。如何正确发送?

1 个答案:

答案 0 :(得分:0)

您似乎没有正确添加FormData并且您的活动似乎已被破坏,您应该在ajax中添加数据,如下所示:

$('#editUser').on('submit', function(event) {
    event.preventDefault();

    var form = $(this); // You need to use standard JS object here
    var formData = new FormData(form);

    $.ajax({
        type: 'PATCH',
        url: '/administrator/users/1',
        data: formData,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function(data) {
            alert(data);
        },
        error:  function(xhr, str){
            alert(str);
        }
    });
});

希望这有帮助!