无法在Laravel中通过Ajax上传图像

时间:2019-01-06 11:52:37

标签: ajax laravel laravel-5


iam尝试通过laravel中的ajax上传图像。
这是我的js代码:

val df = spark.read.table("zen.intent_master").filter(input_file_name.rlike("your regex string"))

这是控制器代码:

$('#profile_picture').on('submit', function(event){
    event.preventDefault();
    $.ajax
    ({
        type: "POST",
        url: "{{url('all/update-profile-picture')}}", 
        data:new FormData(this),
        dataType:'json',
        type:'post',
        processData: false,
        contentType: false,
        cache:false,
    }).done( function(data){
      //swal("Good job!", "Your information has been successfully updated!", "success")
          console.log('Ajax was Successful!')
          console.log(data)
    }).fail(function(xhr, textStatus, error){
        console.log(textStatus)
        console.log(error)
    });
});

我希望这是我的控制者的问题。单击带有空白的提交时,其在控制台中看到的错误消息。
我不明白是什么问题?

1 个答案:

答案 0 :(得分:1)

您需要使用file()方法获取请求文件。

$validation = Validator::make($request->all(), [
    'profile_photo'=> 'required|image|mimies:jpeg,png,jpg,gif|max:2048'
]);

if ($validation->passes()) {

    $new_name = time().'.'.$request->file('profile_photo')->getClientOriginalExtension();
    $request->file('profile_photo')->move(public_path("photo"),$new_name);

    return response()->json([
        'message' => 'Image uploaded successfully'
    ]);
} 

return response()->json([
    'message' => $validation->errors()->all(),
    'profile_photo' => '',
    'class_name' => 'danger'
]);