Laravel图片上传 - AJAX不要发布我的图片文件



我正在尝试将图像从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;
});

最后,当我发布我的表单时,我的帖子数据(来自谷歌开发控制台):

_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;
});

最新更新