jeudi 29 mars 2018

Retrieve file uploaded in Laravel using JavaScript FormData

Running into a bit of trouble trying to upload multiple files through a single AJAX request into a Laravel 5 back-end.

In the front-end, I am using the following code to prep the FormData object:

var fd = new FormData();
fd.append("data", JSON.stringify(values));
fd.append("page_id", page_id);
files.forEach(function(file, index){
    fd.append("file_"+index, file);
});

Then this is my AJAX call:

$.ajax({
    type: 'POST',
    url: '/file_test',
    data: fd,
    contentType: false,
    processData: false,
    success: function(response){
        alert(response);
    },
    failure: function(response){
        alert(response);
    }
});

In the back-end, I've tried to retrieve with $request->allFiles() and $request->file('file_0') but they are both turning up empty.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire