如何在Laravel控制器中处理图像请求

时间:2019-07-18 01:12:28

标签: jquery ajax laravel laravel-5.8

我有一种形式,其中一些输入字段和两个字段用于图像,一个图像将设置为配置文件,第二个图像设置为背景封面,所以我已经处理了一个输入字段,但是我很困惑,不知道如何目前正在执行此操作,我正在使用以下给出的代码

html代码

<form class="emp-form">

    <div class="col-lg-8">
        <label>Website Link</label>
        <input type="text" value="{{$data->website}}" class="form-control profile_web">
    </div>

    <div class="col-lg-8">
        <label>Contact Number</label>
        <input type="number" min="10" max="20" value="{{$data->mobile}}" class="form-control profile_num">
    </div>

    <div class="col-lg-8">
        <label>Company Tagline</label>
        <input type="text" value="{{$data->companytag}}" class="form-control profile_tag">
    </div>

    <div class="col-lg-8">
        <label>Address</label>
        <textarea class="form-control height-140 profile_add" placeholder="Address">{{$data->officeddress}}</textarea>
    </div>
    <div class="col-md-10">
        <label>Company Logo (Must Be 80x80)</label>
        <div class="custom-file-upload">
            <input type="file" id="file" name="image" class="image"/>
        </div>
    </div>

    <div class="col-md-10">
        <label>Company Cover</label>
        <div class="custom-file-upload">
            <input type="file" id="file" name="cimage" class="cimage"/>
        </div>
    </div>

    <div class="form-group text-center emp-process ">
        <button type="button" class="btn theme-btn full-width btn-m update_profile">Save</button>
    </div>
</form>

jQuery代码

$(".update_profile").click(function () {

    var profile_web = $(".profile_web").val();
    var profile_num = $(".profile_num").val();
    var profile_tag = $(".profile_tag").val();
    var profile_add = $(".profile_add").val();
    var comp_name = $(".cname").val();
    var formdata = new FormData();

    formdata.append('image', $(".image")[0].files[0]);
    formdata.append('cimage', $(".cimage")[0].files[0]);
    formdata.append('profile_web', profile_web);
    formdata.append('profile_num', profile_num);
    formdata.append('profile_tag', profile_tag);
    formdata.append('profile_add', profile_add);
    formdata.append('comp_name', comp_name);

    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: '/profile-up',
        type: 'POST',
        data: formdata,
        processData: false,
        contentType: false,
        cache: false,
        dataType: 'JSON',
        enctype: 'multipart/form-data',
        success: function (data) {
            if (data.success) {
                window.location.href = '/dashboard';
            }
        },
        error: function (data) {
            console.log('Error:', data);
        }
    });
});

控制器代码

public function profile(Request $request)
{
    if ($file = $request->file('image')) {

        $name = $file->getClientOriginalName();
        $path = $file->move('images', $name);
        $company = input::get('comp_name');

        companies::where('companyName', $company)->update([
            'website'      => $request->profile_web,
            'mobile'       => $request->profile_num,
            'companytag'   => $request->profile_tag,
            'officeddress' => $request->profile_add,
            'companylogo'  => $path,
        ]);

        return response()->json(['success' => true]);
    }
}

现在我在这里感到困惑,如何在同一功能中处理第二个请求并更新图像。

0 个答案:

没有答案