0

I have a form I am trying to submit with an image file.

The problem is that no image file is being sent.

My AJAX call:

$(document).on('submit', ".hidden-image-upload", function(e){
    e.preventDefault();
    $.ajax({
        url:'/project/uploadImage',
        data: new FormData($(".hidden-image-upload")[0]),
        headers: {
           'X-CSRF-Token': $('form.hidden-image-upload [name="_token"]').val()
        },
        dataType:'json',
        async:false,
        type:'post',
        processData: false,
        contentType: false,
        success:function(response){
            console.log(response);
        },
    });
});

And then my form:

{!! Form::open(['class' => 'hidden-image-upload', 'files' => true]) !!}
    {!! Form::file('file', ['class' => 'cover-image-upload-button']) !!}
{!! Form::close() !!}

In my controller I am just returning:

return $request->all();

And I am getting:

_token: "lFHIf7wiYI3IWqrbcpKxgJEPtXCIVpLm5nVhJ1Ks", file: {}}
_token: "lFHIf7wiYI3IWqrbcpKxgJEPtXCIVpLm5nVhJ1Ks"
file: {}

Any help?

Lovelock
  • 7,689
  • 19
  • 86
  • 186
  • Checkout this post, maybe it could help: http://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax – Yoram de Langen Dec 08 '15 at 07:38

1 Answers1

0

If you return the data, it will be displayed as you are getting. Instead, if you use print_r to print the data submitted, you will get something like this:

Array ( [_token] => IFQtwfwAKbPs8NJ9Sao4X19Z5bzsPzT0SxL0GaUt [file] => Symfony\Component\HttpFoundation\File\UploadedFile Object ( [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => File.csv [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => text/csv [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 169 [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0 [pathName:SplFileInfo:private] => /tmp/phpTT84Kb [fileName:SplFileInfo:private] => phpTT84Kb ) )

To save the uploaded file somewhere else, you should access it like this:

$request->file('file')

More here

Vikas
  • 993
  • 1
  • 10
  • 16