【问题标题】:Laravel form validation in image upload with ajax使用ajax上传图片时的Laravel表单验证
【发布时间】:2016-06-25 03:25:04
【问题描述】:

我正在使用 laravel 5。并使用刀片和 ajax 上传图片。 除非我在控制器内的 store 函数中插入验证代码,否则一切正常。获取服务器错误:

POST http://localhost:8000/imgC 500 (Internal Server Error)

我猜想 ajax 或路由中的 url 有问题,我使用的是 Restfull 控制器。

image.blade.php

{{Form::open(array('url' => 'imgC', 'method' => 'post','files'=>true, 'id'=>'upload_form'))}}
   Title: {{Form::text('title')}}
   Image: {{Form::file('image')}}
   {{Form::submit('submit',['id' => 'btnAddProduct'])}}
   {{Form::close()}}

ImageController.php:

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'name' => 'required|max:255',
    ]);

    if ($validator->fails()) {
        return "error";
    }
    $destinationpath = public_path() . '/img/';
    $image=$request->input('image');
    $filename=$request->file('image')->getClientOriginalName();
    $request->file('image')->move( $destinationpath,$filename );


    $img= new Image;
    $img->name= $request->input('title');
    $img->picture_path=$filename;

    $saveflag=$img->save();
    if($saveflag){
        return Response::json(['success' => $saveflag, 'file' => asset($destinationpath.$filename)]);
    }

}

AJAX 函数:

$(document).ready(function() {
  $('#upload_form').submit(function (event) {
      event.preventDefault();
      $.ajax({
          url: '/imgC',
          data: new FormData($(this)[0]),
          type: "POST",
          processData: false,
          contentType: false
      }).done(function (response) {
          console.log(response);
          $("#success").show();
          setTimeout(function() { $("#success").hide(); }, 5000);

      });
  });
});

route.php:

Route::resource('imgC', 'ImageController');

我在这里做错了什么?

【问题讨论】:

    标签: ajax validation laravel laravel-5 routes


    【解决方案1】:

    我查看了服务器日志文件并弄清楚了。 添加 Use Validator 后验证出现错误;在图像控制器中,问题解决了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 2022-11-03
      • 2020-07-04
      • 2022-12-09
      • 2018-12-15
      • 2015-11-16
      • 1970-01-01
      相关资源
      最近更新 更多