简体   繁体   English

laravel图片上传-Ajax不要发布我的图片文件

[英]laravel image upload - ajax do not post my image file

I'm trying to upload an image from an ajax call to my server (on laravel). 我正在尝试将来自ajax调用的图像上传到服务器(在laravel上)。

This is my server code: 这是我的服务器代码:

if (Input::hasFile('flyer')){
    Log::info('WORK!');
    $file = Input::file('flyer');
    $file->move('uploads', $file->getClientOriginalName());
}

As you can see, I've set a log istruction to understand if my file was uploaded or not...but my log file say it's not... 如您所见,我设置了一个日志指令以了解我的文件是否已上传...但是我的日志文件说不是...

My form code: 我的表格代码:

{{ 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() }}

and here is my ajax call: 这是我的ajax电话:

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

Finally here my post-data (from google dev console) when i post my form: 最后,当我发布表单时,这里是我的发布数据(来自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

As you can see there is no trace of my image file (flyer)...why? 如您所见,没有我的图像文件(传单)的痕迹...为什么?

you cant submit a image like this, you need enctype="multipart/form-data" , try the below code. 您不能提交这样的图像,需要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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM