brainlet's avatar

Why same file upload Ajax code doesn't work in 5.7

Laravel
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet" type="text/css">

<!-- Styles -->
<style>
    html, body {
        background-color: #fff;
        color: #636b6f;
        font-family: 'Nunito', sans-serif;
        font-weight: 200;
        height: 100vh;
        margin: 0;
    }

    .full-height {
        height: 100vh;
    }

    .flex-center {
        align-items: center;
        display: flex;
        justify-content: center;
    }

    .position-ref {
        position: relative;
    }

    .top-right {
        position: absolute;
        right: 10px;
        top: 18px;
    }

    .content {
        text-align: center;
    }

    .title {
        font-size: 84px;
    }

    .links > a {
        color: #636b6f;
        padding: 0 25px;
        font-size: 12px;
        font-weight: 600;
        letter-spacing: .1rem;
        text-decoration: none;
        text-transform: uppercase;
    }

    .m-b-md {
        margin-bottom: 30px;
    }
</style>

@if (Route::has('login')) @auth Home @else Login
            @if (Route::has('register'))
                <a href="{{ route('register') }}">Register</a>
            @endif
        @endauth
    </div>
@endif

<div class="content">
    <div class="title m-b-md">
        Laravel
    </div>

    <form class="form-horizontal file-upload" method="post" enctype="multipart/form-data">
        {{ csrf_field() }}
        <input required="" type="file" name="import_file" id="import_file" />
        <br>
        <br>
        <button class="btn btn-success btn-sm" id="upload_csv" type="submit">Import CSV or Excel File</button>
    </form>
</div>
$(document).ready(function(){ $('form').submit(function(event){
        event.preventDefault();
        var formData = new FormData($(this)[0]);
        $.ajax({
            headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') },
            url: "/form",
            data: formData,
            type: 'post',
            async: false,
            processData: false,
            contentType: false,
            success:function(response){
                // console.log(response);
                alert('uploaded');
            }
        });

    });
});

This scripts works in Laravel 5.4 but NOT in Laravel 5.7, Why ?

0 likes
4 replies
AliceGray's avatar

@brainlet do you get your file when loging formData before ajax request?

console.log(formData);
$.ajax({ ... });
brainlet's avatar

Yes they show up in JS console but does not in PHP

Please or to participate in this conversation.