Laravel Ajax表单提交(图片上传)

时间:2015-01-26 14:32:41

标签: php jquery ajax laravel laravel-4

我正在制作提交需要上传的数据和图片的表单。我正在用php框架做的所有事情都称为Laravel。我收到错误POST http://xxx/admin/addPost 500 (Internal Server Error)这是后端的一些错误,我为JSON响应做了控制台日志并得到了这个{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function move() on a non-object","file":"\/home\/sportcaf\/public_html\/app\/controllers\/admin\/AdminPostsController.php","line":61}}

所以在文件AdminPostsController.php的第61行,我有这段代码:

$uploadFlag = $image->move('uploads',$imageName);

剩下的代码是:

$date = date("m-d-Y");
$image = Input::file('featured_image'); //Image from posted data
$imageName = $slug."-".$date.".jpg";
$imageThumbName = $slug."-".$date."_thumb.jpg";

$uploadFlag = $image->move('uploads',$imageName); //Here is problem
$img = Image::make('uploads/'.$imageName)->fit(200); //Intervention image package
$img->save('uploads/'.$imageThumbName);

我有表格:

<form id="postForm" action="http://xxx/admin/addPost" method="POST" enctype="multipart/form-data">
    Title: <input type='text' name='title'> <br>
    Featured image: <input type='file' name='featured_image'> <br>

    <section id="editor">
      <textarea name='content' id='edit' style="margin-top: 30px;">

      </textarea>
    </section>      
    <input type='submit' value='Publish' name='submit'>
</form>

这是我的jQuery(ajax)代码:

$('#postForm').on('submit', function(){

    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[name]').each(function(index, value){
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

        data[name] = value;
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response){
            console.log(response);
        },
        error: function(response){
            console.log(response);
        }
    });

    return false;
});

0 个答案:

没有答案