图片未上传到服务器上

时间:2019-05-16 07:40:41

标签: php jquery ajax laravel laravel-5.8

我正在使用Laravel 5.8通过ajax将文件上传到服务器上,但不知道出了什么问题。除了输入文件,其他所有数据都将被发送。

hasFile()方法始终返回false,而input :: file()返回null。

我知道这个问题已经问了很多遍了,但是没有一个解决方案对我有用。

jQuery

$(document).ready(function () {

$('#updateUser').click(function(){

    var form = $('#updateForm');

    var postData = new FormData($("#updateForm")[0]);

    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        cache : false,
        contentType: false,
        processData: false,
        url: form.attr('action'),
        type: form.attr('method'),
        dataType: 'json',
        data: postData,
        success : function(response){
            console.log(response);
        }
    });
})

})

查看文件

<form id="updateForm" class="ui form register-form"  method="POST" action="{{ route('updateUserProfile') }}" enctype="multipart/form-data">
    @csrf

    <div class="field">
        <label>Profile Photo</label>
        <div class="two fields">
            <div class="field">
                <img src="{{ $current_user->profile_photo }}">
            </div>
            <div class="field">
                <input type="file" name="profile_photo" placeholder="Profile Photo" class="" multiple="">
            </div>
        </div>
    </div>

控制器文件

namespace App\Http\Controllers\Auth;
use Auth;
use App\Functions;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Input;


public function updateUserProfile(Request $request)
{
    $validators = Validator::make($request->all(), [
        'first_name'    => ['required', 'string', 'max:255'],
        'last_name'     => ['required', 'string', 'max:255'],
        'email'         => ['required', 'string', 'email', 'max:255'],
        'password'      => ['required', 'string', 'min:8', 'confirmed'],
        'mobile_no'     => ['required', 'string', 'max:255'],
        'dob'           => ['required', 'string', 'max:255'],
        'gender'        => ['required', 'string', 'max:255'],
        'country_id'    => ['required', 'string', 'max:255'],
        //'profile_photo' => ['image', 'mime_content_type(jpeg,png,jpg,gif,svg)', 'max:1024'],
        'profile_photo' => 'image'|'mimes:jpg,jpeg,png',
    ]);

    if ($validators->fails()) {
        $result['success'] = false;
        $result['messages'] = $validators->errors()->all();

        return json_encode($result);
    }

    var_dump(input::file('profile_photo'));
    var_dump($request->hasFile('profile_photo'));
}

1 个答案:

答案 0 :(得分:-1)

将此行var_dump($ request-> file('profile_photo'))放入您的代码中