laravel + html ajax 多表单字段和图片一起上传

$("#article_push").on('click', function (e){
            e.preventDefault();
            var stylestr = $('#summernote').eq(0).summernote('code');
            $("#hidden_content").val(stylestr);
            var formData = new FormData($('#article_info'));
            var fileObj  = document.getElementById('thumb').files[0];
            formData.append('title', $("input[name='title']").val());
            formData.append('brief', $("input[name='brief']").text());
            formData.append('content',stylestr);
            formData.append('thumb',fileObj);
            $.ajax({
                headers: {
                  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                url: '/admin/article/create' ,
                data: formData ,
                processData:false,
                contentType: false,
                cache: false,
                success:function(data){
                  if(data.status){
                      layer.msg('发布成功!', {icon: 6});
                  }else{
                      layer.msg('发布失败!', {icon: 5});
                  }
                },
                error:function(err){
                  console.log(err);
                }
            });
        });

  

public function create(Request $request)
    {   dd($request->file('thumb'));
        $status = Article::create($request->except('thumb'));
        if($status){
            return response()->json([
                'status'=>true, 
                'message'=>'发布成功!'
            ]);
        }else{
            return response()->json([
                'status'=>fasle, 
                'message'=>'发布失败!'
            ]);
        }
        
    }

 

posted @ 2017-11-30 09:32  winyh  阅读(951)  评论(0编辑  收藏  举报