Php laravel图像上传-ajax不发布我的图像文件

Php laravel图像上传-ajax不发布我的图像文件,php,ajax,laravel,image-upload,multipartform-data,Php,Ajax,Laravel,Image Upload,Multipartform Data,我正在尝试从ajax调用上传一个图像到我的服务器(在laravel上) 这是我的服务器代码: if (Input::hasFile('flyer')){ Log::info('WORK!'); $file = Input::file('flyer'); $file->move('uploads', $file->getClientOriginalName()); } 正如你所看到的,我已经设置了一个日志指令来理解我的文件是否上传了…但是我的日志文件说它不是

我正在尝试从ajax调用上传一个图像到我的服务器(在laravel上)

这是我的服务器代码:

if (Input::hasFile('flyer')){
    Log::info('WORK!');
    $file = Input::file('flyer');
    $file->move('uploads', $file->getClientOriginalName());
}
正如你所看到的,我已经设置了一个日志指令来理解我的文件是否上传了…但是我的日志文件说它不是

我的表格代码:

{{ Form::open(array('url' => 'new_event', 'id' => 'newEvent', 'files' => true)) }}
    //some fields...
    {{ Form::file('flyer', array('class' => 'form-control', 'id'=>'inputImage')) }}
    // some other fields
{{ Form::close() }}
下面是我的ajax调用:

$('#newEvent').submit(function() {
    $.ajax({
        data: $(this).serialize(),
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        success: function(response) {
            $('#content').html(response);
        }
    });
    return false;
});
最后,这里是我发布表单时发布的数据(来自google开发控制台):

_token:bqyiuQwtYEEs0tvhBcndi4ylsVPPi2FpYhGPLxKQ
title:sdfgsdf
description:sdfgsdf
activated_at:23/04/2015
expire_on:24/04/2015
contact:a name
phone:+387646
email:some@one.com
push:1

正如您看到的,没有我的图像文件(传单)的痕迹…为什么?

您不能提交这样的图像,您需要
enctype=“multipart/form data”
,请尝试以下代码

$('#newEvent').submit(function() {

    // need to get form data as below
    var formData = new FormData($(this)[0]);

    $.ajax({
        data: formData,
        contentType: false,
        processData: false,
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        success: function(response) {
            $('#content').html(response);
        }
    });
    return false;
});

您更好地使用它似乎很有趣,也许我需要它来实现我的下一个功能…但是由于K.Toress answer完成了这个技巧,现在,我推迟了该库的实现。谢谢你和所有的朋友!你为什么要写
$(this)[0]
,而不是只写
this